Validate email address for particular suffix using regex in Ruby-on-Rails -
this question has answer here:
i want validate email address particular suffix should email. must have .edu or must have @mypost.com. example, have 4 email addresses shown below:
1) john@learn.edu - valid
2) john@mypost.com - valid
3) john@gmail.com - invalid
4) john@dblist.org - invalid
i have tried solve problem using below code in user.rb model file no avail.
if ('xc@dblist.com' =~ /\a[\w]([^@\s,;]+)@((mypost|[\w-]+\.)+(edu|com))\z/i) != nil return true else return false end
you can use regex:
\a\w([^@\s,;]+)@(mypost\.com|[\w-]+\.edu)\z see demo (i use ^/$ demo in multilingual mode there)
the (mypost\.com|[\w-]+\.edu) part matches .edu domain , mypost.com.
note if not use captured groups, use \a\w[^@\s,;]+@(?:mypost\.com|[\w-]+\.edu)\z.
Comments
Post a Comment