0

I am using javascript for password validation:

$.validator.addMethod('mypassword', function(value, element) {
    return this.optional(element) || (value.match(/^(?=.*\d)(?=.*[A-Z])[0-9a-zA-Z]{8,}$/));
},
'Password must contain at least one numeric and one uppercase alphabetic character.');

I need the password to be at least one numeric and one uppercase alphabetic character. But, if i enter the characters such as @, # ,$ with one numeric and one uppercase alphaberic character in password, it still shows the error message.

Password9 accepted
Password@1 not accepted 
Kiz
  • 429
  • 5
  • 14
Steve
  • 1,622
  • 5
  • 21
  • 39

3 Answers3

1

This part in your regex does not allow @:

[0-9a-zA-Z]

If you don't want any limit, then change [0-9a-zA-Z]{8,} to .{8,}.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

According to your regular expression, your password must consist of at least 8 alphanumeric characters and nothing else. Your second password does not match this criterion. Try

/^(?=.*\d)(?=.*[A-Z]).{8,}$/

Yet I am not sure this is what you want, as you may want to restrict the set of permitted characters in order to avoid, e.g. non-english letters like äöü.

You might want to list all permitted characters, like so

/^(?=.*\d)(?=.*[A-Z])[0-9a-zA-Z\%\&\/\$\@\#]{8,}$/

Or try to specify a unicode range:

Javascript + Unicode regexes

Community
  • 1
  • 1
JohnB
  • 13,315
  • 4
  • 38
  • 65
1

(function(){  
       var regex=/^(?=.*\d)(?=.*[A-Z])[!-~]{8,}$/;    
    var arr=["Password9","Password@1","password@1"];
    for(var i=0;i<arr.length;i++){
     console.info(arr[i]+"\t"+(regex.test(arr[i])?"accepted":"not accepted"));
    }   
    })(); 

try it

Mr.lin
  • 99
  • 3