0

I'm using this regex to validate e-mail addresses, and it works fine. However, it checks for a dot after the @, but that means that it will allow an address like this: [email protected]... I need to change it so that it checks for a dot before the tld - so that -com won't validate:

/[ÆØÅæøåA-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[ÆØÅæøåA-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[ÆØÅæøåA-Za-z0-9-]*[ÆØÅæøåA-Za-z0-9])?\.)+[ÆØÅæøåA-Za-z0-9](?:[ÆØÅæøåA-Za-z0-9-]*[ÆØÅæøåA-Za-z0-9])?/

Fiddle here.

Anyone knows how to fix that?

2
  • 1
    Don't reinvent the wheel. Look at the long list of related questions on the right. Commented Jun 7, 2013 at 9:23
  • 1
    You haven't specified what language you're working in. Regex questions should include a reference to the langauge you're using, since regex implementations vary between languages. Also, many languages have email address validation libraries available that you should use instead of regex; in general regex is the wrong tool for this; if an alternative is available you should use it. Commented Jun 7, 2013 at 10:06

2 Answers 2

1

Well, then don't allow it in the last part of the regex:

/…(?# something that ends with a dot)[ÆØÅæøåA-Za-z0-9](?:[ÆØÅæøåA-Za-z0-9-]*[ÆØÅæøåA-Za-z0-9])?/
                                                                        _^_

and simplify that to

/…[ÆØÅæøåA-Za-z0-9]+/

(Btw, Stop Validating Email Addresses With Complicated Regular Expressions)

Sign up to request clarification or add additional context in comments.

6 Comments

Interesting. Can you write the whole regex, please?
I'm not sure it's working. See fiddle here: jsfiddle.net/qX45r/2 It should allow 2 dots after @ as long we always require a dot before the tld.
you are going to loose more than 50% of your customers with that regex..:P
Well, you somehow managed to get an invisible \u8204 between the last + and /. Works now
|
0

An email address is valid if you can send a message to it.Even space is a valid character in email address

There are various conflicting standards for email and many vendors never followed them.

So you can't just make your own criteria for validating an email.Even if you make one you would loose many of your customers

You are better off using this regex

.+@.+

After validating email with above regex you can send a message to that email and wait for response from the user.If email address is valid you would receive a response,if not then you can assume that the email address is invalid


Have a look at these articles

1 Comment

I completely agree in your opinion (see the link I posted in my answer), but the OP's question is valid - there are really no TLDs with dashes inside.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.