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 badges1 Answer
Reset to default 9you 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_]