3

So in my registration form I have this field:

 <div class="form-group">
    <label for="RegisterModel_Password">Password</label>
    <input type="password" id="RegisterModel_Password" 
           name="RegisterModel.Password" class="form-control" 
           required="required" minlength="8"/>
</div>

As you see, I'm using jQuery validation attributes to ensure that the password includes at least 8 characters. So, I want to check if password contains uppercase and number, if not, field is not valid. I downloaded additional method for jQuery Validation plugin named "pattern" and added her in head tag.

I tried to do this as follows but it didn't worked.

$("#formRegister").validate({
    rules: {
        RegisterModel_Password: {
            pattern: /^[a-zA-Z][0-9]/
        }
    }
});

I assume that the pattern is wrong, but I'm not sure whether the use is correct. Thank you for your help.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Bardr
  • 2,319
  • 2
  • 12
  • 21
  • 1
    Here, some pattern documentation bro: https://www.sitepoint.com/jquery-basic-regex-selector-examples/ Hope it helped! ;) – SrAxi Mar 20 '17 at 12:04
  • http://stackoverflow.com/questions/14850553/javascript-regex-for-password-containing-at-least-8-characters-1-number-1-uppe – Muhammed Imran Hussain Mar 20 '17 at 12:05
  • Possible duplicate of [Password REGEX with min 6 chars, at least one letter and one number and may contain special characters](http://stackoverflow.com/questions/7844359/password-regex-with-min-6-chars-at-least-one-letter-and-one-number-and-may-cont) – Muhammed Imran Hussain Mar 20 '17 at 12:06
  • Please don't enforce such a rule. Normally I generate passwords with `head -c 18 /dev/urandom | base64` and sometimes there is no digit, or no upper case character in the result. That's just a property of randomness that you can't exactly tell how the output looks like. Nevertheless the the password strength is 18 bytes. You may assume that your users are not stupid. – Kijewski Mar 20 '17 at 12:08
  • 3
    @Kay *" You may assume that your users are not stupid. "* hahaha....hahahaha... oh boy! good one :) – freedomn-m Mar 20 '17 at 12:13

2 Answers2

10

Chains of regular expressions are too hard for me ( I have never tried to learn them lol ). So here is my solution:

jQuery.validator.addMethod("passwordCheck",
        function(value, element, param) {
            if (this.optional(element)) {
                return true;
            } else if (!/[A-Z]/.test(value)) {
                return false;
            } else if (!/[a-z]/.test(value)) {
                return false;
            } else if (!/[0-9]/.test(value)) {
                return false;
            }

            return true;
        },
        "error msg here");

And simply I use it like a attribute:

<input type="password" id="RegisterModel_Password" 
name="RegisterModel.Password" 
class="form-control" 
required="required" minlength="8" 
passwordCheck="passwordCheck"/>

Thanks for your answers.

Bardr
  • 2,319
  • 2
  • 12
  • 21
  • 2
    I think this is a better solution than using regex. I would still add a `value.length >= 8` test for completeness, if the browser does not enforce the `minlength` attribute. – Kijewski Mar 21 '17 at 14:06
0

You can add your custom validation using $.validator.addMethod() like:

$.validator.addMethod("validation_name", function(value) {
    // at least 1 number and at least 1 character
    [^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
});
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59