Rails Validation for Username and Email -
Rails Validation for Username and Email -
i have next validation rules username , email.
validates :username, presence: {:message => "please take username.", on: :update}, uniqueness: {:message => "username exisits. please select different one."}, length: {in: 3..15, :message => "username should 3-5 characters long", :allow_blank => true} validates :email, presence: {:message => "please come in email address."}, uniqueness: {:message => "email address exisits. please login!"} validate :email_regex def email_regex if email.present? , not email.match(/\a[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/) errors.add :email, "please come in valid email address." end end
for username: not want allow other - little letters, dot, numbers.
do need write different validate message email_regex or there simplied version can utilize appending exisiting rules comma ','
you can utilize like:
validates :username, format: { with: /\a([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i}
add comma separate alternative format existing validation username. 1 more thing missed, dummy regex, need apply per purpose.
ruby-on-rails validation
Comments
Post a Comment