开发者

Using jquery with regex for validaton

开发者 https://www.devze.com 2023-03-12 06:25 出处:网络
I am having a really hard time using Regex with the jquery validate plugin. My code is: jQuery.validator.methods.nameCheck= function(value) {

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  = funct开发者_如何学JAVAion(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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消