0

How to update the following regular expression /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/ to accept the "+" in the email address.

Now:

abc@gmail.com // true

abc+100@gmail.com // false

What I need

abc@gmail.com // true

abc+100@gmail.com // true

My Code:

export const handleEmailValidation = (email) => {
  const validEmailAddress = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  const containAt = /^((?!@).)*$/;
  const lastAt = /^[a-z|A-Z|0-9]+[^@]\s?@{1}$/;
  if (containAt.test(String(email).toLowerCase())) {
    return 'An email address must contain a single @ ';
  }
  if (lastAt.test(String(email).toLowerCase())) {
    return 'Please enter a valid value after the @ ';
  }
  if (!validEmailAddress.test(String(email).toLowerCase())) {
    return 'Please enter a valid email address';
  }
  return '';
};
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Khaled Ramadan
  • 161
  • 3
  • 13
  • 1
    Add `+` in the first bracket pair: `[a-zA-Z0-9_\-\.+]` – Kévin Bibollet Jun 07 '19 at 08:24
  • 2
    Or..... Stop using regex to attempt a "perfect" parsing of email addresses. This regex is good though for 99% of scenarios: `^[^@]+@[^@]+$` -- and if you really want to be certain that an address is correct, *send a confirmation email*. – Tom Lord Jun 07 '19 at 08:29
  • 1
    @TomLord I agree — any practical regex will be sure to leave out *some* email addresses, and IMO it's much better to get some false emails than to lose someone because they're told their email is invalid when they know it works – Jojodmo Jun 07 '19 at 08:31
  • @TomLord Thanks for you comment. But my question is more how to update the presented regex. – Khaled Ramadan Jun 07 '19 at 08:33
  • @KhaledRamadan There are literally thousands of posts about this in StackOverflow, and elsewhere on the internet. There are even [entire websites dedicated to the question](https://emailregex.com/). If you want to go ahead and copy+paste one of those crazy regexes to "solve" the problem, then go ahead -- but be aware that every single one of them is flawed. – Tom Lord Jun 07 '19 at 08:35

2 Answers2

1

Add the + character to the first character set:

/^([a-zA-Z0-9_\-\.+]+)...
                  ^
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can use simply the following regex;

^[\w.+\-]+@gmail\.com$

[
\W -  Matches any word character (alphanumeric & underscore).
.  -  Matches a "." character.
+  -  Matches a "+" character. 
\  -  Matches a "-" character.
]
+  -  Match 1 or more of the preceding token.
@  -  Matches a "@" character. 
gmail - Matches gmail characters.
\. -  Matches a "." character.
com - Matches com characters.
$ -  Matches the end of the string.

RegExpr example

You can add 'i' modifier that means "ignore case"

var regex = new RegExp('^[\w.+\-]+@gmail\.com$', 'i');
console.log(regex.test("abc@gmail.com"));
console.log(regex.test('abc+100@gmail.com'));
Dontwan
  • 121
  • 6
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Jun 07 '19 at 10:08