Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails email validation with name and email

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.

like image 553
neo-code Avatar asked Dec 30 '25 17:12

neo-code


1 Answers

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

like image 56
Soundar Rathinasamy Avatar answered Jan 01 '26 07:01

Soundar Rathinasamy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!