Link to home
Start Free TrialLog in
Avatar of Stoke
Stoke

asked on

alphabetic and numeric character check

I am looking for a regEx that checks a password for me. It has to confirm the password must contain at least one alphabetic character and must contain at least one numeric character.

Thank you for your time and help.
Avatar of nedfine
nedfine
Flag of India image

$_ =shift;
if($_ =~/[a-z]/ && $_ =~/\d+/)
{
#  "correct format";
}
else
{
#  "wrong format";
}
ASKER CERTIFIED SOLUTION
Avatar of nedfine
nedfine
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brett Crawley
Brett Crawley

This will test if there is at least 1 alphabetic character or 1 numeric character. If you want to fix a minimum length just increase the value 2 before the comma to the minimum length you require. It also accepts most punctuation characters.

\A(?=[!-/:-@[-\x60{-\x7E\x80a-zA-Z0-9]*?[a-zA-Z])(?=[!-/:-@[-\x60{-\x7E\x80-_a-zA-Z0-9]*?[0-9])\S{2,}\z

If you don't want to accept punctuation or want to modify which may be accepted you simply need to edit from the ! to the \x80 in each of the character classes [] before the *?

Regards


This solution doesn't use the hex values so it might be a bit clearer to my previous one.

\A(?=[!-/:-@\[-`{-~€a-zA-Z0-9]*?[a-zA-Z])(?=[!-/:-@\[-`{-~€-_a-zA-Z0-9]*?[0-9])\S{2,}\z
/\d/ && /[[:alpha:]]/
in Perl would define alphabetic according to the current locale