I am trying to validate a text input. I have noticed though that it fails if there are any spaces in the text field.
validates_format_of :role, :with => /\A[a-zA-Z]+\z/, :message => "field should only have letters"
What do I need to change to allow spaces? Thanks
Update:
Changed question to anything but numbers, as I need to allow special characters in the text field.
Just add a space to the characters you are matching (at the moment
a-zandA-Z). HenceNote that
\swill match any whitespace character (including tabs and newlines).Update
To allow everything but numbers, you can use a “negated character class”, which is made by putting a caret
^at the start of the square brackets:or, since
\d(for “digit”) is equivalent to[0-9],