java - Regex for password matching -


i have searched site , not finding looking for. password criteria:

  1. must 6 characters, 50 max
  2. must include 1 alpha character
  3. must include 1 numeric or special character

here have in java:

public static pattern p = pattern.compile(  "((?=.*\\d)(?=.*[a-z])(?=.*[a-z])|(?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_]).{6,50})" ); 

the problem password of 1234567 matching(it valid) should not be.

any great.

make sure use matcher.matches() method, assert whole string matches pattern.

your current regex:

"((?=.*\\d)(?=.*[a-z])(?=.*[a-z])|(?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_]).{6,50})" 

means:

  • the string must contain @ least digit (?=.*\\d), lower case english alphabet (?=.*[a-z]), , upper case character (?=.*[a-z])
  • or | string must contain @ least 1 character may digit or special character (?=.*[\\d~!@#$%^&*\\(\\)_+\\{\\}\\[\\]\\?<>|_])
  • either conditions above holds true, , string must between 6 50 characters long, , not contain line separator.

the correct regex is:

"(?=.*[a-za-z])(?=.*[\\d~!@#$%^&*()_+{}\\[\\]?<>|]).{6,50}" 

this check:

  • the string must contain english alphabet character (either upper case or lower case) (?=.*[a-za-z]), , character can either digit or special character (?=.*[\\d~!@#$%^&*()_+{}\\[\\]?<>|])
  • the string must between 6 , 50 characters, , not contain line separator.

note removed escaping characters, except [], since {}?() loses special meaning inside character class.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -