I have a text box in my web form. In jQuery I have to verify that the entered text should have at least one lowercase and one uppercase letter. How does the pattern look?
- What have you tried?ghoti– ghoti2012-03-16 14:10:14 +00:00Commented Mar 16, 2012 at 14:10
- 1That searches for a string that starts with 5 characters that may be any of upper or lower case letters. It doesn't require anything beyond that. See answers below.ghoti– ghoti2012-03-16 14:30:34 +00:00Commented Mar 16, 2012 at 14:30
Add a comment |
3 Answers
Assuming ERE:
/([A-Z].*[a-z]|[a-z].*[A-Z])/ or if you're a purist:
/([[:upper:]].*[[:lower:]]|[[:lower:]].*[[:upper:]])/ 8 Comments
yunzen
How good is the POSIX notation compatible to the browsers, do you know?
ghoti
@yunzen - JavaScript supports Perl-compatible regular expressions. You can compare flavours and their capabilities if you like. If you're asking whether PCRE supports Character Classes, the answer is yes, it does.
yunzen
@ghoti but my flavor of Chrome RegEx does not support the
:lower: and :upper: POSIX Character Classes. It supports :Alpha: and :alpha:yunzen
@tchrist Thanks for the link. I'll savor this for me.
|
/[a-z].*[A-Z]|[A-Z].*[a-z]/ Test here: http://www.regular-expressions.info/javascriptexample.html (without the /)
Comments
//TODO check Number var checkNumber = false var matches = currentPassword.match(/\d+/g) ||newPassword.match(/\d+/g) || confirmPassword.match(/\d+/g) ; if (matches != null) { checkNumber = true; } //TODO check Letter var checkLetter = false var matchesLetter = currentPassword.match("[a-z\A-Z]") || newPassword.match("[a-z\A-Z]") || confirmPassword.match("[a-z\A-Z]") ; if (matchesLetter != null) { checkLetter = true; } //TODO check upper and lower Letter var checkUpperLowerLtr = false var matchesUpperLowerLtr = currentPassword.match("[a-z].*[A-Z]|[A-Z].*[a-z]") || newPassword.match("[a-z].*[A-Z]|[A-Z].*[a-z]") || confirmPassword.match("[a-z].*[A-Z]|[A-Z].*[a-z]"); if (matchesUpperLowerLtr != null) { checkUpperLowerLtr = true; } //TODO Special var checkSpecial = false var matchesSpecial = currentPassword.match("/?[#?!@$%^&*-]") || newPassword.match("/?[#?!@$%^&*-]") || confirmPassword.match("/?[#?!@$%^&*-]") ; if (matchesSpecial != null) { checkSpecial = true; }