Looking for a Rails validation that will only allow letters, numbers, and spaces. This will do letters and numbers, but no spaces. I need spaces.
validates_format_of :name, :with => /^\w+$/i,
:message => "can only contain letters and numbers."
validates_format_of :name, :with => /^[a-zA-Z\d ]*$/i,
:message => "can only contain letters and numbers."
Here is only Number, Letters ans spaces.
Is that exactly what you need ?
PS : This tools is very useful if you are doing a lot of reg-exp : http://rubular.com/
This is one way:
validates :name, format: { with: /\A[a-zA-Z0-9\s]+\z/i, message: "can only contain letters and numbers." }
Have a nice day.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With