用于浮点数和整数验证的JavaScript [英] JavaScript for float and integer number validation
问题描述
我试图从一个文本框中为验证整数值
做一个javascript函数。什么是验证它的最好方法,以便只有 integer和float
值是可以接受的?
必需的java脚本函数用于数字验证。
//移除空格
var input = input .replace(/ \s + /克, );
//检查输入是否是有效的数字
if(isFinite(input)&& input!=''){
//做你的事
$ b 请记住,isFinite只接受'20 .50'而不是'20,50'在一些国家是习惯的。如果你需要这种灵活性,你需要做额外的字符串预处理。这个解决方案只允许有空格作为千位分隔符(例如'100 000')。
不幸的是,检查一个空字符串是必要的,因为isFinite('')返回true。
您也可以使用此功能从用户 CMS (详细解释请参阅:验证JavaScript中的十进制数字 - IsNumeric()) / p>
函数isNumber(n){
return!isNaN(parseFloat(n))&& isFinite的(N);
}
I tried to make a javascript function to validate integer values
from a text box. What is the best way to validate it so that only integer and float
values are acceptable?
Required java script function for number validation.
解决方案 // remove whitespaces
var input = input.replace(/\s+/g,"");
// check if the input is a valid number
if(isFinite(input) && input != ''){
// do your thing
}
Remember that isFinite only accepts values like '20.50' and not '20,50' as is custom in some countries. If you need this kind of flexibility you need to do additional string preprocessing. And with this solution only spaces are allowed as thousand delimiters (e.g '100 000').
Unfortunately the check for an empty string is necessary since isFinite('') returns true.
You could also use this function from user CMS (for a detailed explanation see: Validate decimal numbers in JavaScript - IsNumeric())
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
这篇关于用于浮点数和整数验证的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!