Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Validations, number + letters + spaces

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."
like image 614
thenengah Avatar asked Feb 28 '23 11:02

thenengah


2 Answers

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/

like image 148
Nicolas Guillaume Avatar answered Mar 07 '23 06:03

Nicolas Guillaume


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.

like image 36
Ivica Lakatoš Avatar answered Mar 07 '23 04:03

Ivica Lakatoš