Reputation: 367
I need create a regular expression in ruby on rails for validate the user name following this rules:
1)- The user name accept only lowercase letters, numbers,dashes and underscores
2)- The first character must be a lowercase letter
3)- The username have as minimum 6 characters and 30 characters as maximun
I thought some like this, but I'm not very sure
/^ ([a-z][a-z\_-0-9]+){6,30} $/
Upvotes: 0
Views: 206
Reputation: 19539
I'd suggest the following:
/^[a-z][a-z\-0-9_]{5,29}$/
I'll break it down:
^[a-z]
- the sequence starts with (^
) a lowercase character[a-z\-0-9_]
- any of the chars in that group{5,29}
- between 5 and 29 (30 max, minus the one at the start of the sequence) of the previous set of chars$
the end of the sequenceYou mentioned character classes - sure, you can use them. I typically don't because they're not as portable (someone may chime in and prove me wrong here) and I don't think they're as easy to understand, simply because I tend to forget which ones are which. YMMV
Upvotes: 3