0

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.

1
  • This is a handy tool I use to check my regex. Might help with this or in the future when you are working with regex. http://www.fileformat.info/tool/regex.htm Commented Jun 9, 2011 at 16:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I dont think my boss "trusts" jquerys validate, so I threw both in for good measure. Im getting there, thank you!
well whatever you want to do keep in mind that you should always do a double check server side
so to use this would you just do the following? $('#myForm').validate({ rules: { "myInput": { alphanumeric: true, ..., ..., }
@JeffSilva yes or you could just add it as a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.