user3269780
user3269780

Reputation: 127

Jquery Form Validator multiple regexes issue

I want to add multiple types of regex on the password field of my form. I have done it as follow and its working. The problem is when i type something into it gives me error message even when i have entered the lowercase and digits in the field.

  $.validator.addMethod "LowserCase", ((value, element, regexp) ->
    return this.optional(element) || /[a-z]+/.test(value);
  ), "Password should have atleast one lowercase letter"

  $.validator.addMethod "Uppercase", ((value, element, regexp) ->
    return this.optional(element) || /[A-Z]+/.test(value);
  ), "Password should have atleast one uppercase letter"

  $.validator.addMethod "Digit", ((value, element, regexp) ->
    return this.optional(element) || /[0-9 -()+]+$/.test(value);
  ), "Password should have atleast one digit"

$("#user_password").rules "add",
  required: true
  minlength: 8
  Uppercase: true
  Digit: true
  LowserCase: true

Can you please tell me why it is behaving like that. ? Or how can i validate multiple regex and show different error messages

Thanks

Regards

Upvotes: 0

Views: 1736

Answers (2)

raisul
raisul

Reputation: 640

You can try creating validation rules against class. You can add a rule for a specific class like this:

$.validator.addMethod(      // add a validation method
    'letterSpaceFieldRegEx',
    function (value) {
        return /^([a-zA-Z\ ])*$/.test(value); // only letters and space
    },
    '* Only letters'
);
jQuery.validator.addClassRules('letterField', { // custom rule for class 'letterField'
    letterSpaceFieldRegEx: true
});

Now you can use the class 'letterField' for validation.

Such as:

$("#user_password").addClass('letterField');

Upvotes: 0

Sparky
Sparky

Reputation: 98738

Your problem has nothing to do with regex at all. You have a whole bunch of syntax issues and I'm not sure how any of this could have been working. You're missing all kinds of punctuation, like parenthesis, braces, commas, and the word function itself.


Your .rules() method...

$("#user_password").rules "add",
  required: true
  minlength: 8
  Uppercase: true
  Digit: true
  LowserCase: true

Should look like this...

$("#user_password").rules("add", {  // <-- opening braces and parenthesis
    required: true,   // <-- a comma after ever rule
    minlength: 8,
    Uppercase: true,
    Digit: true,
    LowserCase: true
});   // <-- closing braces and parenthesis

See: http://jqueryvalidation.org/rules/


You have a similar problem on each of your custom methods....

$.validator.addMethod "LowserCase", ((value, element, regexp) ->
    return this.optional(element) || /[a-z]+/.test(value);
), "Password should have atleast one lowercase letter"

Should be...

$.validator.addMethod("LowserCase", function(value, element) {
    return this.optional(element) || /[a-z]+/.test(value);
}, "Password should have atleast one lowercase letter" );

Please look at the exact placement of the braces and parenthesis throughout and the usage of the word function. You also don't need to pass the third argument (regexp) into the function since you're not using it in the function.

See: http://jqueryvalidation.org/jQuery.validator.addMethod/

(Note, the word "Lower" in your "LowerCase" function name is misspelled as "Lowser", although your code is working because you are consistent when using this LowserCase spelling error. Also note, there is no such word as "atleast"; It's always spelled out with two words as "at least".)


Working DEMO: http://jsfiddle.net/WCq27/


EDIT:

Regex for Digit rule should be changed to this...

/[0-9]+/.test(value);

New DEMO: http://jsfiddle.net/WCq27/1/

Upvotes: 1

Related Questions