0

I know this has been asked a million times, but I just can't seem to crack it.

I have this:

function checkPassword(strPassword)
{
  var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");

  var blnResult = objPattern.test(strPassword);
  return(blnResult)
}

...but it only seems to check the length, and not if there's a number?

What have I missed?

Edit:

The number can be anywhere in the string, not necessarily at the end.

ojsglobal
  • 525
  • 1
  • 6
  • 31

3 Answers3

4

Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
0

You can just include both tests separately in your function:

function checkPassword(strPassword){
  var blnResult = /\w{6,}/.test(strPassword)
   && /\d+/.test(strPassword);
  return(blnResult)
}

Demo:

function checkPassword(strPassword){
  var blnResult = /\w{6,}/.test(strPassword)
   && /\d+/.test(strPassword);
  return(blnResult)
}

var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
 console.log(p+" ::: "+ checkPassword(p));
});
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$