I want to validate a string using jQuery.match() function. String must contain
- Minimum of 10 characters.
- Must contains atleast one numeral.
- Must contain atleast one capital letter.
How can I do that? Can anyone show me the regular expression to do that?
I am already have this:
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$
but it is validating like this only, allow atleast one special character, one uppercase, one lowercase(in any order).
I want to validate a string using jQuery.match() function. String must contain
- Minimum of 10 characters.
- Must contains atleast one numeral.
- Must contain atleast one capital letter.
How can I do that? Can anyone show me the regular expression to do that?
I am already have this:
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$
but it is validating like this only, allow atleast one special character, one uppercase, one lowercase(in any order).
Share Improve this question edited Sep 19, 2013 at 9:54 Rohit Jain 213k45 gold badges414 silver badges533 bronze badges asked Sep 19, 2013 at 9:54 CoolCool 3482 gold badges11 silver badges32 bronze badges 03 Answers
Reset to default 5It is not strictly conforming to the length restriction, because you haven't done it correctly. The first look-ahead - (?=.{8,})
, is just testing for string with minimum length 8
. Remember, since the look-arounds are 0-length assertions, the look-aheads after .{8,0}
are not consuming any character at all.
In fact, you can remove that first look-ahead, and simply use that quantifier at the end while matching.
Try this regex:
^(?=.*[A-Z])(?=.*[0-9]).{10,}$
Break up:
^
(?=.*[A-Z]) # At least an uppercase alphabet
(?=.*[0-9]) # At least a numeral
.{10,} # Any character 10 or more times
$
I'm not sure how you got that regex; it seems to have been taken somewhere...
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$
^^^^ ^^^^^ ^^^^^ ^--------^
1 2 3 4
Makes sure there's at least 8 characters
Makes sure there's lowercase characters
Makes sure there's uppercase characters
Makes sure there are those special characters.
To make a regex to your requirements, do some changes:
^(?=.{10})(?=.*[0-9])(?=.*[A-Z]).*$
^^^^ ^^^^^ ^^^^^
1 2 3
Makes sure there's at least 10 characters
Makes sure there's at least a number.
Makes sure there's at least an uppercase letter.
You can make it a bit shorter using:
^(?=.*[0-9])(?=.*[A-Z]).{10,}$
^ # Start of group
(?=.*\d) # must contain at least one digit
(?=.*[A-Z]) # must contain at least one uppercase character
. # match anything with previous condition checking
{10,} # length at least 10 characters
$ # End of group
i.e.:
^(?=.*\d)(?=.*[A-Z]).{10,}$
Source:
- Password matching expression