使用javascript进行十进制验证 [英] decimal validation using javascript
本文介绍了使用javascript进行十进制验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的要求是文本框里面有网格。对于这个文本框,我需要这样的验证 - 在十进制用户之前应该能够输入5位数(不应该允许输入超过5位)和小数后3位数。如何使用javascript
函数IsDecimal(evt){
debugger;
var charCode =(evt.which)? evt.which: event .keyCode
if (charCode > 31 &&(charCode < 48 || charCode > 57 )&& charCode != 46 )
return false 跨度>;
else {
var len = evt。值跨度>。长度;
var index = evt。 value .indexOf(' 。');
if (index > 0 && charCode == 46 ){
return < span class =code-keyword> false ;
}
if (index < 0 ){
if (len > 5 ){
return false ;
}
}
如果(index > ; 0 ){
var CharAfterdot =(len + 1 ) - 索引;
var CharBeforeDot =(len + 1 ) - CharAfterdot;
if (CharAfterdot > 4 ){
return false ;
}
if (CharBeforeDot > 5 ){
return false ;
}
}
}
返回 真跨度>;
}
这里允许用户在十进制前输入到5位数。但问题是在我尝试放入之后dot是十进制的。它不允许用户输入文本框
解决方案
试试这个:
< !DOCTYPE html >
< html >
< body >
文本框:
< ; 输入 type < span class =code-keyword> = text onkeyup = 验证(this); onkeydown = 验证(this); > ;
< script >
函数验证(e){
var isValidCharacter = true;
e.value = e.value.trim();
var inputValue = e.value;
if(isNaN(inputValue)){
isValidCharacter = false;
} else if(inputValue.length == 6&& inputValue.substr(inputValue.length - 1)!='。'){
isValidCharacter = false;
} else if(inputValue.length> 9){
isValidCharacter = false;
}
if(isValidCharacter == false){
e.value = inputValue.substring(0,inputValue.length - 1);
}
}
< / script >
< / body >
< / html >
尝试使用正则表达式
< pre lang =Javascript> var regex = / ^ [0-9] { 0 , 5 }(?:\。[0-9] { 0 , 3 跨度>})</ BLOCKQUOTE>
/?;
return regex.test(evt.value);
Hi,
my requirement is text box is there inside grid. For this textbox, i need validation like this-- before decimal user should able to enter 5 digit(should not allow to type more than 5) and after decimal 3 digit. How to do using javascript
function IsDecimal(evt) { debugger; var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) return false; else { var len = evt.value.length; var index = evt.value.indexOf('.'); if (index > 0 && charCode == 46) { return false; } if (index < 0) { if (len > 5) { return false; } } if (index > 0) { var CharAfterdot = (len + 1) - index; var CharBeforeDot = (len + 1) - CharAfterdot; if (CharAfterdot > 4) { return false; } if (CharBeforeDot > 5) { return false; } } } return true; }
Here its allowing to user to enter till 5 digit before decimal.But problem is after that i m trying to put dot which is decimal.Its not allowing to enter to user into textbox解决方案Try this:
<!DOCTYPE html> <html> <body> Textbox: <input type="text" onkeyup="validate(this);" onkeydown="validate(this);"> <script> function validate(e) { var isValidCharacter = true; e.value = e.value.trim(); var inputValue = e.value; if(isNaN(inputValue)) { isValidCharacter = false; } else if (inputValue.length == 6 && inputValue.substr(inputValue.length - 1) != '.') { isValidCharacter = false; } else if (inputValue.length > 9) { isValidCharacter = false; } if(isValidCharacter == false) { e.value = inputValue.substring(0, inputValue.length - 1); } } </script> </body> </html>
try with regex
var regex = /^[0-9]{0,5}(?:\.[0-9]{0,3})?
/; return regex.test(evt.value);
这篇关于使用javascript进行十进制验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文