I think its simple but I am too dumb to write one. Can someone proovide me with a regular expression that checks a given string for atleast 1 letter and atleast 1 number.
Also please give some explanation.
I think its simple but I am too dumb to write one. Can someone proovide me with a regular expression that checks a given string for atleast 1 letter and atleast 1 number.
Also please give some explanation.
This would obviously be much easier with two checks, one for the string, one for the number.
Something like the following might work though, seeing as if you have both a number and a letter then you must by definition have a number next to a letter (or vice versa):
([A-Za-z][0-9]|[0-9][A-Za-z])
Update: removed a spurious '|'. Note, the above assumes no other characters are valid, which I suppose might not be acceptable. See other answer for a better solution if punctuation is allowed.
REs aren't that good for very complicated things like multiple orders but this simple one should be representable with:
[A-Za-z].*[0-9]|[0-9].*[A-Za-z]
A best way would be separately check for this, using 2 regex: /[a-z]/i
and /\d/
.
Else, assuming you want to enforce the security rule for a password, split it (e.g. in PHP with str_split
) and count the number of occurence of each character type, which will give you an estimation of the password strength and let you adapt your rules.
Performance is not an issue if this for password change, you don't do it on 20K stings...