Validating email address in RoR
I am trying to validate an email address user has entered. I've done the validation in model but its not working. I have used the following in model...
class Employee < Acti开发者_开发问答veRecord::Base
belongs_to :department
validates_presence_of :emp_name_official
validates_presence_of :emp_name_full
validates_format_of :emp_email_personal, :with=>/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message=>"Not a valid email format"
end
I am using version 2.0.2 of rails.
And it works fine when I comment the validation line in my model. And also works fine when I give correct email address.
Can anyone tell where I am going wrong.
Following is the error page.
I've just sorted out. It is the problem with my all the validations. If any validation fails, It throws the above no method error. I'll work on it why this sort of error is coming. I'll open a separate question for it.
Thanks to all of you for all responses and directions.
Try this
EmailRegex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates_format_of :email, :with => EmailRegex
Could it be something to do with the trialling 'i' after your regex?
精彩评论