I’m trying to create a regex for correct username validation in Ruby on Rails but for some reason I am doing something incorrect where all username inputs are invalid. I am allowing for upper-case characters, lower-case characters, numbers, and underscores.
Ruby on Rails name validation regex code:
validates :name, :format => {:with => /\A[A-Za-z\d_]\z/}
What am I doing wrong?
Thanks
This is your expression. Do you really want to match a single character?
I believe that :
this is what you are looking for. \w is a shorthand character class for what you have written. The above regex will match a string which consists only of A-Za-Z0-9_