最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regex to strictly check alphanumeric and special character - Stack Overflow

programmeradmin5浏览0评论

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

Share Improve this question edited Sep 18, 2013 at 3:53 Ravi asked Sep 18, 2013 at 3:44 RaviRavi 31.4k44 gold badges124 silver badges180 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 2

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}$/

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}$/;
发布评论

评论列表(0)

  1. 暂无评论