Ben Totoro
Ben Totoro

Reputation: 63

Using regex in an additional jQuery Validate method

I need to create an additional method for the jQuery validate plugin, but I'm not great with regular expressions. What I need is to check if a text input is typed like I50xxxx OR i50xxxx, where xxxx are alphanumerics (0000 to 9999)

I made this :

jQuery.validator.addMethod("matricule", function(value, element) {
    return this.optional(element) || /^I50\d{4}$/.test(value);
}, "Entrez un matricule valide (I50xxxx)");

It's Ok to check the first type, but not both. Can you help me?

Upvotes: 0

Views: 37

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Add i modifier in order to perform a case-insensitive search.

/^I50\d{4}$/i.test(value);

or

/^[iI]50\d{4}$/.test(value);

Upvotes: 2

Related Questions