qinking126
qinking126

Reputation: 11875

Jquery validation plugin question

i need to validate the account number, it can be either 6 number (111111), or two letter then 6 numbers (xx111111).

Can someone please show me how to do it? thank you.

Upvotes: 2

Views: 100

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

As @Teja says, you need to use .addMethod with a regular expression. I believe this should work:

$.validator.addMethod("accountnumber", function(value, element) {
    return this.optional(element) || /^(\w{2})?\d{6}$/.test(value);
}, "Please enter a valid account number");


$("#form").validate({
    rules: {
        account: {
            accountnumber: true
        }
    }

});

Example: http://jsfiddle.net/andrewwhitaker/tUaB7/

Upvotes: 4

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

Use the addMethod method on the validation plugin as shown in the example and modify the regex to match your criteria..

Upvotes: 0

Related Questions