Rajat
Rajat

Reputation: 1

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

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?

Upvotes: 0

Views: 2905

Answers (3)

slipset
slipset

Reputation: 3078

Use jQuery validator: Source

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

Upvotes: 1

Gowri
Gowri

Reputation: 16835

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 !

Upvotes: 0

OneOfOne
OneOfOne

Reputation: 99274

Something along the lines of :

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

Upvotes: 1

Related Questions