0

I have a regex for emails validation and it goes like this:

[A-Z0-9a-z._%+-]+@([A-Za-z0-9]+[.-]?)+[A-Za-z0-9]+\.[A-Za-z]{2,}

But some users with a single letter after the @ character email can not be validated. For instance: [email protected] is not valid.

I'm not sure how to change the regex to allow this validation as well.

By checking it, this part here @([A-Za-z0-9] should already allow setting at least one character after @ but it doesn't. If I put like at least 2 letters then the email is validated. For instance: [email protected] is valid.

Any help is more than welcome.

2
  • 3
    Why craft your own? Commented May 2, 2023 at 11:32
  • 1
    Your pattern enforces at least two characters. That is because ([A-Za-z0-9]+[.-]?)+ will require at least one, and followed by [A-Za-z0-9]+ which requires another character Commented May 2, 2023 at 11:36

1 Answer 1

1

Maybe you can try this way [A-Z0-9a-z._%+-]+@([A-Za-z0-9]+[.-]?)*[A-Za-z0-9]+\.[A-Za-z]{2,}

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

2 Comments

An explanation could be nice... Especially considering you only changed a single character from the original pattern...
Thank you, now I will try to help people with a step-by-step explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.