Ortal
Ortal

Reputation: 343

Using jquery with regex for validaton

I am having a really hard time using Regex with the jquery validate plugin. My code is:

jQuery.validator.methods.nameCheck  = function(value) {
  return  /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/i.test(value);
};

jQuery.validator.methods.emailCheck  = function(value) {
      return  /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(value);
};

jQuery.validator.methods.AddressRegex  = function(value) {
      return    /^[a-zA-Z0-9][#&-\'\,\.]*$/i.test(value);
};

jQuery.validator.methods.lettersonly = function(value, element) {
  return this.optional(element) || /^[a-zA-Z]*$/i.test(value);
}; 

jQuery.validator.methods.PasswordRegex = function(value) {
      return /^[a-zA-Z0-9\,!#$%^&*()_-+\.]+$/i.test(value);
}; 

The first two work, but even lettersonly, which seems simple enough, isn't doing what it is supposed to do. They are being called properly further down the form.

Upvotes: 0

Views: 1524

Answers (1)

mcgrailm
mcgrailm

Reputation: 17640

here is an example of alphanumeric that as i use it if you want just alpha remove the 0-9

jQuery.validator.addMethod("alphanumeric", function(value, element) { 
    return  this.optional(element) || value.match(/^[a-zA-Z0-9]+$/);
}, "This field may only contain alpha numeric characters.");

that should get you started just duplicate and adjust using your regex's

ohh and I would just use validate's email rather than build your own

Upvotes: 1

Related Questions