-1

I want to display error message if user enter three dots likesteve@gmail.com.co.nz so after .co if user use .nz Error should be thrown because user has already entered two dots. Below is my Email validation which i am using right now

var EmailValid = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9.-]+[A-Za-z0-9'\.\-\s\,\#\]*(\\.[A-Za-z]{2,3})$";

Any idea how to do that?

Steve
  • 352
  • 2
  • 6
  • 24

1 Answers1

0

This is what I came up with, based on your Regex and the condition that there can be no more than three . after the @ sign:

^[\w-+]+(?:\.[\w-]+)*@[\w-]+\.[\w'\-\s,#\\]*\.?(?:[A-Za-z]{2,3})$

Notes:

  • I swapped out your _A-Za-z0-9 for \w as it is the same thing.
  • I moved the . matching out of the quantifiers (i.e., + and *) as you have a requirement to match between 1-2 max .s after the @ sign.
  • I kept most of your required matches for emails, even though I've never seen such symbols used in email endings before. So there may be room for improvement there.

Railroad Diagram:

enter image description here

Regex101

timolawl
  • 5,434
  • 13
  • 29
  • @Steve can you provide me an example of cases that do not work? I have linked the Regex101 link with example cases that work and do not work following your use case. – timolawl May 06 '16 at 16:26