Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest regular expression to validate emails to not accept them blindly? [closed]

When users create an account on my site I want to make server validation for emails to not accept every input.

I will send a confirmation, in a way to do a handshake validation.

I am looking for something simple, not the best, but not too simple that doesn't validate anything. I don't know where limitation must be, since any regular expression will not do the correct validation because is not possible to do it with regular expressions.

I'm trying to limit the sintax and visual complexity inherent to regular expressions, because in this case any will be correct.

What regexp can I use to do that?

like image 316
eKek0 Avatar asked Apr 12 '09 21:04

eKek0


People also ask

What is simplest regular expression for email validation?

Regex : ^(.+)@(.+)$ This one is simplest and only cares about '@' symbol. Before and after '@' symbol, there can be any number of characters. Let's see a quick example to see what I mean.

What is the regular expression for email?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times.

Which of the following regex is used to validate an email address?

With that in mind, to generally validate an email address in JavaScript via Regular Expressions, we translate the rough sketch into a RegExp : let regex = new RegExp('[a-z0-9]+@[a-z]+\.

How do you validate a regular expression?

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false ( === false ), it's broken. Otherwise it's valid though it need not match anything. So there's no need to write your own RegExp validator.


1 Answers

It's possible to write a regular expression that only accept email addresses that follow the standards. However, there are some email addresses out there that doesn't strictly follow the standards, but still work.

Here are some simple regular expressions for basic validation:

Contains a @ character:

@ 

Contains @ and a period somewhere after it:

@.*?\. 

Has at least one character before the @, before the period and after it:

.+@.+\..+ 

Has only one @, at least one character before the @, before the period and after it:

^[^@]+@[^@]+\.[^@]+$ 

User AmoebaMan17 suggests this modification to eliminate whitespace:

^[^@\s]+@[^@\s]+\.[^@\s]+$ 

And for accepting only one period [external edit: not recommended, does not match valid email adresses]:

^[^@\s]+@[^@\s\.]+\.[^@\.\s]+$ 
like image 122
Guffa Avatar answered Sep 21 '22 11:09

Guffa