Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with Regex password verification with default android keyboard layouts such as Japanese and Russian keyboards

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading