开发者

how to check only 4 or 16 numeric digits in the input box using jquery?

开发者 https://www.devze.com 2023-02-15 07:36 出处:网络
i am creating a web page where I have a input text field in which I want to check only 开发者_如何学运维4 or 16 numeric digit

i am creating a web page where I have a input text field in which I want to check only 开发者_如何学运维4 or 16 numeric digit

How can i make it using jquery?


Something along the lines of :

$('#form').submit(function(){
    var val = $('#someinput').val();
    if(/^(\d{4}|\d{16})$/.test(val) == false) {
        // do something
        return false;
    }
}
);


Use jQuery validator: Source

$("#myform").validate({   rules: {
    field: {
      required: true,
      minlength: 4,
      maxLength:16
    }   } });


if your looking for jquery validation this methode will help you

jquery validation(http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

$.validator.addMethod("textfield", function(value, element) {
            return this.optional(element) || /^(\d{4}|\d{16})$/ i.test(value);
        }, "<br>You must enter 4 digit or 16-digit");

Please clarify your question !

0

精彩评论

暂无评论...
验证码 换一张
取 消