验证jQuery的数字文本字段 [英] Validate numeric text field in jquery
问题描述
我在jQuery来prevent非数字字符此code被输入到文本字段
I have this code in jquery to prevent non-numeric characters being inputted to the text field
$("#NumericField").numeric();
现在,该文本字段我不能输入非数字字符。那没问题。这里的问题是,如果用户将与非数字字符的文本字段中粘贴。
Now, on the text field i cant input non-numeric characters. That is OK. The problem here is if the user will paste on the text field with non numeric characters.
有没有办法/方法来禁用粘贴如果值为非数字?或者有没有其他的方法来处理这种情况,你可以分享?
Is there a way/method to disable pasting if the value is non-numeric? Or is there any other approach to handle this situation that you can share?
推荐答案
您可以使用回调哪些检查在离开现场,如果值是有效的,如果值无效然后将其清除,并显示错误消息:
you can use callback which checks on leaving field if value is valid if value is not valid then clear it and show error message:
var decimal_char = ',';
function isvalidnumber(){
var val=$(this).val();
//This regex is from the jquery.numeric plugin itself
var re=new RegExp("^\\d+$|\\d*" + decimal_char + "\\d+");
if(!re.exec(val)){
alert("Invalid number");
$(this).val("");
}
}
$(document).ready(function(){
$("#txtN").numeric(decimal_char,isvalidnumber);
});
这篇关于验证jQuery的数字文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!