I used Regex to verify a password with a minimum of 8 characters containing 1 uppercase, 1 lowercase, 1 special character, and 1 numeric value. The problem I encountered is that if people from Russia or any other country whose language is different from English try to enter the password using the default keyboard, it will create a problem for them.
Currently i have set this regex condition for my application :
String regex = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
It works if the user sets the password in English. But it does not work if the user's language is different. (A password like Испытание@123
will fail).
I searched on google about russian regex for the default keyboard and found a solution for the russian keyboard.
Do i need to work out for all different language for password validation regex?
Is there an alternative solution so that I can check the password check for any default keyboard layout with the password validation that I want?
CodePudding user response:
You can use \p{Ll}
to match any Unicode lowercase letters and \p{Lu}
to match any Unicode uppercase letters:
String regex = "^(?=\\P{Lu}*\\p{Lu})(?=\\P{Ll}*\\p{Ll})(?=[^0-9]*[0-9])(?=[^#?!@$%^&*-]*[#?!@$%^&*-]).{8,}$";
Note the .*?
in the lookaheads is not efficient, I used the reverse patterns (\P{Lu}
matches any char that is not a Unicode uppercase letter, \P{Ll}
matches any char other than a Unicode lowercase letter, etc.) in line with the principle of contrast.
If you need to support any Unicode digits, too, replace (?=[^0-9]*[0-9])
with (?=\\P{Nd}*\\p{Nd})
where \P{Nd}
matches any char other than a Unicode digit and \p{Nd}
matches any char other than a Unicode decimal digit.