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 bination
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
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 bination
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
- You have the underscore withing the regexp parameters. – user2417483 Commented Sep 18, 2013 at 3:47
- yes.. i considered underscore as special character – Ravi Commented Sep 18, 2013 at 3:49
- I don't understand what you are trying to achieve. Maybe some more examples of what you want to return false. – user2417483 Commented Sep 18, 2013 at 3:51
- only accept alphanumeric and special character,which is mentioned in the regex expression other than that it should return false. I have also mentioned two example for right input and 3 example of wrong input – Ravi Commented Sep 18, 2013 at 3:54
3 Answers
Reset to default 2You 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}$/
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.
The dollar sign is a reserved character for Regexes. You need to escape it.
var regex = /^[a-zA-Z0-9_/$@.]{8,15}$/;