A regular expression for an alphanumeric password with at least 1 number and a character

I need help with a regular expression for an alphanumeric password with at least 1 number and a character, and the length should be between 8-20 characters.

I have this, but it doesn't seem to work (it also has no length requirements):

^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$ 
+3
source share
6 answers

If you look at the MSDN link , it gives an example of a RegEx expression for password verification and (more precisely) use it in ASP.NET.

For what you want to accomplish, this should work:

  (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$ 

This requires at least one digit, at least one alphabetic character, no special characters and a length of 8 to 20 characters.

+9
source

Why not just use some simple functions to check?

 checkPasswordLength( String password); checkPasswordNumber( String password); 

Perhaps a few more checks for the appearance of the same character repeatedly and sequentially.

+3
source
 ^(?=.{8,20}$)(?=.*[0-9])(?=.*[a-zA-Z]).* 

? :)

+1
source

Wouldn't it be better to do this check with some simple string functions instead of trying to do a regular expression validation?

0
source

Something like this will be closer to your needs. (I have not tested it).

 Regex test = new Regex("^(?:(?<ch>[A-Za-z])|(?<num>[9-0])){8,20}$"); Match m = test.Match(input); if (m.Success && m.Groups["ch"].Captures.Count > 1 && m.Groups["num"].Captures.Count > 1) { // It a good password. } 
0
source

This code is for javascript

 // *********** ALPHA-Numeric check *************************** function checkAlphaNumeric(val) { var mystring=new String(val) if(mystring.search(/[0-9]+/)==-1) // Check at-leat one number { return false; } if(mystring.search(/[AZ]+/)==-1 && mystring.search(/[az]+/)==-1) // Check at-leat one character { return false; } } 
0
source

Source: https://habr.com/ru/post/1346238/


All Articles