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

javascript - regular expression for username with ^[a-z0-9_-]{3-20}$ - Stack Overflow

programmeradmin4浏览0评论

I am using this ^[a-z0-9_-]{3-20}$ regex for validating usernames..

my requirements

  • it should be between 3-20
  • it should not have special chars except hyphen and underscore
  • it should start with alphabet

what is problem with this regex

  • it checks for 3-20 but it also returns true when string have special chars between 3-20
  • it works (return false {what is expected}) when special chars between 1-3 but it fails(return true {what is not expected}) when special chars between 3-20...

I am using yii framework and default rule match pattern... is it yii fault ...?

I am using this ^[a-z0-9_-]{3-20}$ regex for validating usernames..

my requirements

  • it should be between 3-20
  • it should not have special chars except hyphen and underscore
  • it should start with alphabet

what is problem with this regex

  • it checks for 3-20 but it also returns true when string have special chars between 3-20
  • it works (return false {what is expected}) when special chars between 1-3 but it fails(return true {what is not expected}) when special chars between 3-20...

I am using yii framework and default rule match pattern... is it yii fault ...?

Share Improve this question edited Sep 2, 2013 at 6:04 Michael Härtl 8,6075 gold badges39 silver badges66 bronze badges asked Sep 1, 2013 at 20:36 Jaimin MosLakeJaimin MosLake 6751 gold badge13 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

you can use this:

/^[a-z][a-z0-9_-]{2,19}$/i

You must use a , to write a range in curly brackets quantifiers. I have put an only letter character class at first to follow your specifications (begin with a letter), thus I decrement the quantifier to {2,19}

I assumed you allow uppercase letters and i add an i modifier, but you can remove it if you only allow lowercase letters.

Note that you can write this regex like that:

/^[a-z][\w-]{2,19}$/i

since \w stand for [a-zA-Z0-9_]

发布评论

评论列表(0)

  1. 暂无评论