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
  • 5,760
  • 2
  • 24
  • 34
Mathew
  • 231
  • 1
  • 4
  • 13

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
  • 713,503
  • 59
  • 514
  • 593
1

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

If it works plz vote.

JavaLearner
  • 349
  • 1
  • 3
  • 8