Reputation: 1320
Hi I'm new to this regular expression. Please help me on this query.
I want the Regular expression to match at least One capital letter and at least One digit and any number of special characters. Minimum length 8 and maximum length can be 15.
Note : The special characters allowed are @#$&.
Thanks for your help.
Upvotes: 9
Views: 23234
Reputation: 4428
This should work (unless you want to match newlines too):
/(?:[A-Z].*[0-9])|(?:[0-9].*[A-Z])/
(I missed the length restriction, but anyway you seem satisfied with what you got there.)
Upvotes: 1
Reputation: 1320
Thanks guys. I found the answer.
/^(?=.*\d)(?=.*[A-Z])(?!.*[^a-zA-Z0-9@#$^+=])(.{8,15})$/
Upvotes: 20
Reputation: 6753
Regex:
[A-Z]+[0-9]+[@#\$&]*
And for the length part, use:
var len = str.length;
if( /[A-Z]/.test(str) && /[0-9]/.test(str) && len >= 8 && len <= 15 )
[A-Z]
- one capital letter
[0-9]
- one digit
[abc]
means any of a
, b
, or c
.
Upvotes: 6