[英]Textbox only allow floating point number
Here is the code in html to allow only one decimal point in a textbox:这是 html 中的代码,只允许文本框中有一个小数点:
<html>
<head>
<script type="text/javascript" language="javascript">
function isNumberKey(evt) {
var charCode = (evt.charCode) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
else {
var input = document.getElementById("txtChar").value;
var len = document.getElementById("txtChar").value.length;
var index = document.getElementById("txtChar").value.indexOf('.');
if (index > 0 && charCode == 46) {
return false;
}
if (index >0 || index==0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 2) {
return false;
}
}
if (charCode == 46 && input.split('.').length >1) {
return false;
}
}
return true;
}
</script>
</head>
<body>
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)" name="txtChar" class="CsstxtChar" maxlength="4"/>
</body>
</html>
I want to done this in asp.net using c#.This code is not properly working in asp.net.我想使用 c# 在 asp.net 中完成此操作。此代码在 asp.net 中无法正常工作。
use this it would be helpful....使用它会很有帮助....
$('.urInputField').keyup(function(e){
var val = $(this).val();
var regexTest = /^\d{0,8}(\.\d{1,2})?$/;
var ok = regexTest.test(val);
if(ok) {
$(this).css('background-color', 'green');
} else {
$(this).css('background-color', 'red');
}
});
the id of controls may differ from what you enter in asp.net source for example when you use parent-child controls or use master pages... , So, you can not use document.getElementById simply.控件的 id 可能与您在 asp.net 源中输入的内容不同,例如,当您使用父子控件或使用母版页时...,因此,您不能简单地使用 document.getElementById。
as i see your code is not just to block non-digit keys as other ones suggest duplicate solutions, but it also block backspace or arrow keys and put a limit on number of digits after decimal point such that only one digit is allowed after dot.正如我所看到的,您的代码不仅像其他人建议的重复解决方案那样阻止非数字键,而且还阻止退格键或箭头键并限制小数点后的位数,以便点后只允许一位数字。 i don't change these custom algorithm you used in your code.
我不会更改您在代码中使用的这些自定义算法。
this code get the source element which causes the keypress from event parameters:此代码从事件参数获取导致按键的源元素:
<script type="text/javascript" language="javascript">
function isNumberKey(event) {
var e = event || window.event;
var src = e.srcElement || e.target;
var charCode = e.which || e.keyCode || e.charCode;
//document.getElementById("label").value = src.id; //just for test/debug
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
return false;
else
{
var input = src.value;
var len = input.length;
var index = input.indexOf('.');
if (index > 0 && charCode == 46) return false;
if (index > 0 || index == 0) {
var CharAfterdot = (len + 1) - index;
if (CharAfterdot > 2) return false;
}
if (charCode == 46 && input.split('.').length > 1) {
return false;
}
}
return true;
}
</script>
Use this Source will work good for float numbers使用此 Source 将适用于浮点数
function isNumber(evt) {函数 isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
} }
thanks Vamsi谢谢瓦姆西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.