1

I need to check whether my password contains one number and one character.

Input form

<input class="form-control" data-validate="required,minlength[8],alphaNumeric" id="password"  name="password" placeholder="password" type="text">

Below is my jQuery validate method.

jQuery.validator.addMethod("alphaNumeric", function (value, element) {
    return this.optional(element) || /^[0-9a-zA-Z]+$/.test(value);
}, "password must contain atleast one number and one character");

The problem is, the above regexp is not validating for the criteria where password should contain at least one number and one character.

dda
  • 6,030
  • 2
  • 25
  • 34
Mathew
  • 231
  • 1
  • 4
  • 14

2 Answers2

3

You can use lookahead regex like this to make sure it matches an input with at least 1 digit and 1 alphabet:

/^(?=\D*\d)(?=[^a-z]*[a-z])[0-9a-z]+$/i
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Try this: /^(?=.[0-9])(?=.[a-zA-Z])([a-zA-Z0-9]+)$/

If it works plz vote.

JavaLearner
  • 389
  • 1
  • 3
  • 8