I want to write a validation for email filed .But some different way. I will allow user to enter email in two format like "name '[email protected]' " and simple '[email protected]' .so basically i want to write a validation which will check that is valid email format in present in the value or not.
Just need a custom validation for check the valid email format is present in the input email value.
My model look like :
class Contact < ActiveRecord::Base
validates :email ,presence: true
validate :email_format
def email_format
??? what to write here ???
end
end
How can i write a validation for this.
You need to slightly modify the regular expression in your case.
validates :email, format: { with: /(\A([a-z]*\s*)*\<*([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\>*\Z)/i }
This will match the following formats.
[email protected]
Soundar<[email protected]>
Soundar <[email protected]>
soundar<[email protected]>
Soundar Rathinsamy<[email protected]>
Soundar Rathinsamy <[email protected]>
soundar rathinsamy <[email protected]>
If you need some change continue editing this regular expression at rubular.com
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