1

How can I restrict only numerics in the Email address. I tried below and it didn't work. My email should accept Alpha-numerics, dot, hyphen and underscore followed by "@AnyDomain.com". But it should not accept only numerics like "112233@gmail.com"

Pattern regexPattern = Pattern.compile("^[(a-zA-Z0-9-\\_\\.!\\D)]+@[(a-zA-Z)]+\\.[(a-zA-Z)]{2,3}$");
Matcher regmatcher = regexPattern.matcher(email);
  • Don_123@gmail.com -> Valid
  • 12345@gmail.com -> Invalid
Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27
seleguy
  • 13
  • 2
  • 1
    Run two regexes. The first to catch the common patterns, and the second to filter out ones that are all numbers. – Grumblesaurus Oct 01 '19 at 17:15
  • 1
    Why do you want to validate email addresses (with regex)? What is special about numbers before the '@' character? If you want to check if an email is valid you send an email for confirmation. – Progman Oct 01 '19 at 17:21
  • Accept apostrophes too please! – erickson Oct 01 '19 at 17:29
  • Part of your question is answered here [enter link description here](https://stackoverflow.com/questions/36159017/regex-for-alphanumeric-characters-but-not-only-numeric) – kulsin Oct 01 '19 at 17:44

4 Answers4

1

You can use this regex to validate your condition

^\w*?[a-zA-Z]\w+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z

regex to validate above condition.

abby37
  • 597
  • 6
  • 21
0

I have modified regex the one provided in the question.

[a-zA-Z]+[(a-zA-Z0-9-\\_\\.!\\D)]*[(a-zA-Z0-9)]+@[(a-zA-Z)]+\.[(a-zA-Z)]{2,3}

I have added [a-zA-Z]+ at the beginning to ensure the email address doesn't start with numbers. If the email address can start with the special symbol, make sure you update it in the first block.

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
0

Using regex for email can be problematic. Emails are not as simple as you think - there are many valid characters. Ideally you should let the browser do it input[type="email"] and send your users an activation email in order to prove that the email address is valid but I understand that there may be a legitimate need to validate the address on the server.

There's useful info here: https://stackoverflow.com/a/201378/1921385

Still need an rx?

According to https://emailregex.com/ the regex used by W3C to validate an input[type=email] is:

/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

So you could use this and just take out the number portion:

/^[a-zA-Z.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

Have a play at regex101.com

Here's a demo:

input {border:10px solid red;}
input:valid {border-color:green;}
<input type="text" required="required" pattern="^[a-zA-Z.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" placeholder="type=text, using a pattern" />
<input type="email" required="required" placeholder="type=email, no pattern here" />
Moob
  • 14,420
  • 1
  • 34
  • 47
0

The regex below works for me every time for email verification. However, this might not solve all cases as emails accept a whole range of characters and more recently, different formats. If you're using the email for authentication, I would advise you send a verification email to confirm or add an extra mode of authentication.

/^[a-zA-Z.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

Basically, the strength of a validation regex lies in the number of cases it can accurately capture while simultaneously checking for non-compliance (fakes). Each of these character sequences has a purpose in the overall validation process. You can learn more about regular expressions here.

I hope this helps. Merry coding!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69