Rails Validations, number + letters + spaces - ruby-on-rails

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 only want letters, numbers, and spaces, but not underscores, the accepted answer won't work for you. The following also won't allow empty strings, but it wouldn't matter either way if the rails model has validates_presence_of :name
/^[a-z0-9 ]+$/i

Related

Rails validates_format_of :username

validates_format_of :username, :with => /\A[a-zA-Z_]{2,35}\Z/
I am using the above to validate usernames but I want to change it as it is not good enough for my app. what I need is:
allow letters only or letters with underscore (nothing else)
letters should be all small caps
string does not start or end with underscore (underscore may appear in between letters only)
only one underscore is allowed
username characters limit is 35
I have tried several ways and I could not do it could someone help me out please?
Try following, assuming that "small caps" means lower case:
validates :username, length: { maximum: 35 }, format: { with: /\A[a-z]+_?[a-z]+\z/ }

How can I include Polish letters in a validation regex?

I have validation for first_name:
validates :first_name, :format => {:with => /\A[a-zA-Z]+\z/}
Can somebody tell me how to add letters like:
ą,ż,ź,ć,ń,ł,ś,ę,ó, Ą,Ż,Ź,Ć,Ń,Ł,Ś,Ę,Ó
I think you need Oniguruma character classes. To verify that string consists of unicode letters, use alpha character class.
"abcÓ" =~ /\A[[:alpha:]]+\z/ # => 0
"abcÓ1" =~ /\A[[:alpha:]]+\z/ # => nil # contains digit
This, of course, will include not only said polish letters, but all unicode letters. Including japanese kana, for example (おにぐるま).

validate dots and comma with regex ruby 1.9.2

I have this regex in my model post:
validates_format_of :description, :username, :with => /^(?:[^\W_]|\s)*$/u, :message => "should only contain letters, numbers, or .,-_#"
But this regex does not allow dots "." and commas ",".
I want allow add this characters to this regex.
How can I allow that this regex works fine for fields like username or textareas for validate letters, numbers, commas, dots...etc
validates_format_of :description, :username, :with => /^(?:[^\W_]|\s|[\.,_#])*$/u, :message => "should only contain letters, numbers, or .,-_#"

Validate that string belongs to specific language alphabet

How can I validate Rails model string attribute that it belongs to specific language alphabet characters?
Thanks.
There's a library called whatlanguage that recognize the languages of the string, example:
require 'whatlanguage'
"Je suis un homme".language # => :french
Works with Dutch, English, Farsi, French, German, Swedish, Portuguese, Russian and Spanish out of the box, so it recognize Cyrillic too.
You'll want to validate the value of the attribute against a regular expression.
# Only match characters a-z
validates_format_of :attr, :with => /[a-z]/
validates_format_of seems to be the right thing for you. the documentation says:
Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided.
class Person < ActiveRecord::Base
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
end
Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line.
A regular expression must be provided or else an exception will be raised.

Validate: Only letters, numbers and -

I would like to validate my users, so they can only use a-z and - in their username.
validates_format_of :username, :with => /[a-z]/
However this rule also allows spaces ._#
Username should use only letters, numbers, spaces, and .-_# please.
Any ideas?
Best regards.
Asbjørn Morell
You may need to say the whole string must match:
validates_format_of :username, :with => /^[-a-z]+$/
You may also need to replace ^ with \A and $ with \Z, if you don't want to match a newline at the start/end. (thanks to BaroqueBobcat)
Appending an i will cause it to match in a case-insensitive manner. (thanks to Omar Qureshi).
(I also originally left off the +: thanks to Chuck)
More complex solution but reusable and with more fine grained error messaging.
Custom validator:
app/validators/username_convention_validator.rb
class UsernameConventionValidator < ActiveModel::EachValidator
def validate_each(record, field, value)
unless value.blank?
record.errors[field] << "is not alphanumeric (letters, numbers, underscores or periods)" unless value =~ /^[[:alnum:]._-]+$/
record.errors[field] << "should start with a letter" unless value[0] =~ /[A-Za-z]/
record.errors[field] << "contains illegal characters" unless value.ascii_only?
end
end
end
(Notice it does allow ' . - _ ' and doesnt allow non ascii, for completeness sake)
Usage:
app/models/user.rb
validates :name,
:presence => true,
:uniqueness => true,
:username_convention => true
The [] may contain several "rules" so [a-z0-9] gives lowercase letters and numbers
the special character - must go at the start of the rule
Does
[-a-z0-9#_.]
give the effect you want?
validates_format_of :username, :with => /^[\w\-#]*$/
Note the *, which means '0 or more'
Simply change the regular expression to match all characters your specification states (\w covers all alphanumeric characters -- letters and numbers -- and an underscore).
validates_format_of :username, :with => /[\w \.\-#]+/
Validation to allow letters and whole numbers only:
/\A[a-zA-Z0-9]+\z/

Resources