Reputation: 31437
To check alphanumeric with special characters
var regex = /^[a-zA-Z0-9_$@.]{8,15}$/;
return regex.test(pass);
But, above regex returns true
even I pass following combination
asghlkyudet
78346709tr
jkdg7683786
But, I want that, it must have alphanumeric and special character otherwise it must return false for any case. Ex:
fg56_fg$
Sghdfi@90
Upvotes: 1
Views: 14515
Reputation: 425458
You can replace a-zA-Z0-9_
with \w
, and using two anchored look-aheads - one for a special and one for a non-special, the briefest way to express it is:
/^(?=.*[_$@.])(?=.*[^_$@.])[\w$@.]{8,15}$/
Upvotes: 2
Reputation: 56829
Use look-ahead to check that the string has at least one alphanumeric character and at least one special character:
/^(?=.*[a-zA-Z0-9])(?=.*[_$@.])[a-zA-Z0-9_$@.]{8,15}$/
By the way, the set of special characters is too small. Even consider the set of ASCII characters, this is not even all the special characters.
Upvotes: 2
Reputation: 306
The dollar sign is a reserved character for Regexes. You need to escape it.
var regex = /^[a-zA-Z0-9_/$@.]{8,15}$/;
Upvotes: -1