Jquery仅允许具有唯一且有限数字的数字字段

时间:2016-11-15 07:26:41

标签: javascript jquery html

我希望jquery脚本只允许数字字段唯一编号,只允许4位数字。

HTML

<input type="text" id="registration_number" class="input-sm" name="registration_number" maxlength="6" required> 

我的jQuery代码正在运行,但我也采用重复值。

 jQuery("#job_no").keypress(function(e) { 

    var verified = (e.which == 8 || e.which == undefined || e.which == 0) ? null : String.fromCharCode(e.which).match(/[^0-9]/);
    if (verified || e.delegateTarget.value.length>3 || e.ctrlKey ==true) { if(e.which!=8 ){e.preventDefault();}}

}).on('paste',function(e){ e.preventDefault();});

我是jquery的新手,所以我怎么能这样做?

有人请帮帮我。

1 个答案:

答案 0 :(得分:0)

var myInput = $("#registration_number").val();
if($.isNumeric(myInput)) {
  var myInputNum = parseInt(myInput);
  if(myInputNum < 10000) {
     // do things here
  }

}
相关问题