Reputation: 23
I made this regex code:
/^([a-zA-Z\d]+[_]?+[a-zA-Z\d]){3,12}$/
I want the expression to:
When I test a name with more than 12 chars it still gives me a positive result.
How do I fix this?
Upvotes: 2
Views: 56
Reputation: 627082
You may use
^(?=.{3,12}$)[a-zA-Z\d]+(?:_[a-zA-Z\d]+)?$
Details
^
- start of string(?=.{3,12}$)
- length allowed from 3 to 12 chars[a-zA-Z\d]+
- 1+ letters or digits(?:_[a-zA-Z\d]+)?
- an optional sequence of _
and 1+ digits/digits$
- end of stringUpvotes: 5