允许/验证 jquery 中的整数、小数、浮点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20323475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Allow/validate integer, decimal, float values in jquery
提问by Shiva Krishna Bavandla
I had a html field like below
我有一个像下面这样的 html 字段
<input type="text" name="price" class="validate_price" id="price"/>
So the field should accept only float, decimal, integer values from the input.
因此该字段应该只接受来自输入的浮点数、十进制数、整数值。
jQuery code
jQuery 代码
<script>
function isFloat(value)
{
return value!= "" && !isNaN(value) && Math.round(value) != value;
}
$(document).ready(function(){
$('.validate_price').focusout(function(){
var value = this.value;
if (value)
{
if (!isFloat(value) || !$.isNumeric(value))
alert('Value must be float or not');
}
});
});
</script>
So from the above code, what all my intention is, I should allow user to enter integer values(1,2,2, etc.,)
, float values(1.0, 2.0, 3.0, 3.2, 3.5, etc.)
and decimal values(2.152, 4.56, 5.000, 6.3256, 2.00 etc.,)
所以从上面的代码,我的意图是什么,我应该允许用户输入整数值(1,2,2, etc.,)
,浮点值(1.0, 2.0, 3.0, 3.2, 3.5, etc.)
和十进制值(2.152, 4.56, 5.000, 6.3256, 2.00 etc.,)
How to check/implement the above functionality ?
如何检查/实现上述功能?
回答by Mike
If you notice from the jQuery.isNumeric()
manualmanual, it also validates floats, so there is no need to include any other arguments. To validate a number, all you have to do is use jQuery.isNumeric()
:
如果您从jQuery.isNumeric()
手册中注意到,它还会验证浮点数,因此无需包含任何其他参数。要验证一个数字,您所要做的就是使用jQuery.isNumeric()
:
if (!$.isNumeric(value)) {
alert('Value must be numeric');
}
I made a quick jsFiddle for you: http://jsfiddle.net/guxz9/
我为你做了一个快速的 jsFiddle:http: //jsfiddle.net/guxz9/
回答by LeGEC
Taken from a paragraph in MDN documentation on parseFloat:
摘自MDN 文档中关于 parseFloat的一段:
var filterFloat = function (value) {
if(/^\-?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
回答by jatin
This will valid your all float numbers.It will also valid only single time negative sign('-') and dot(.) symbol.
这将使您的所有浮点数有效。它也将仅对单次负号('-')和点(.)符号有效。
function float_validation(event, value){
if(event.which < 45 || event.which > 58 || event.which == 47 ) {
return false;
event.preventDefault();
} // prevent if not number/dot
if(event.which == 46 && value.indexOf('.') != -1) {
return false;
event.preventDefault();
} // prevent if already dot
if(event.which == 45 && value.indexOf('-') != -1) {
return false;
event.preventDefault();
} // prevent if already dot
if(event.which == 45 && value.length>0) {
event.preventDefault();
} // prevent if already -
return true;
};
<input type="text" onkeypress="return float_validation(event, this.value)" class="form-control"/>
This will validate: -12.33 12.44 12
这将验证:-12.33 12.44 12
回答by Pradeep Kumar Das
$("#Latitude, #Longitude, #Revenue").keydown(function(e) {
debugger;
var charCode = e.keyCode;
if (charCode != 8) {
alert($(this).val());
if (!$.isNumeric($(this).val()+e.key)) {
return false;
}
}
return true;
});