Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Simplest Regular Expression for validating an email
5 Posts
Simplest Regular Expression for validating an email

Hi Guys,

from past two days i am working on some module. In this module, i have to validate the email addresses as well.

I am fade up of using that common email regular expression for the validation of email. I tried for something, and created a simple and very useful regular expression for the email validation.

It covers almost 95% of possibilities of email validation.

var emailRegs=/((\S+)@(\S{3}[a-zA-z0-9)]\S*))/;

Hi,

Try this: email_to_validate[/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}/]

Maricrisnonato121213
well seasoned senior ruby on rails de...
Forum Rating: 100
Sign In To Rate Post

Reply TO:

Hi,

Try this: email_to_validate[/[a-zA-Z0-9._%]@(?:[a-zA-Z0-9].)[a-zA-Z]{2,4}/]

Reply: Hi Maricris, I am agreed with you. But still its not a complete solution. According to rules any email like sam@sam.examp is also valid..........It means its not necessary to be 4 characters after the last '.'. One more thing, to check the validity of any email ID, better way is to use any Messengers chat window, because they usually have validations already applied.

I am no saying that my solution in 100 % correct :). my just saying it can handle 95% of email validations......

By the way, thanks for Regular expression.

I found this one just now. Well, there's still the disclaimer that it won't work 100% of the time, but it looks like a nice solution. They've got one that works with javascript as well.

Read here: http://fightingforalostcause.net/misc/2006/compare-email-regex.php

Maricrisnonato121213
well seasoned senior ruby on rails de...
Forum Rating: 100
Sign In To Rate Post

There are some people that argue against anything more basic than @.* then do a dns check on the domain since, as has been mentioned before, there is no regexp that will validate every possible email format.

Just a possible option

5 Posts
Login to add your message