1

So I'm trying to validate an email address with the following rules: 1 to 64 characters (lowercase, uppercase, digits or .) plus @ plus another 1 to 64 characters (lowercase, uppercase, digits or .)

I tried with this

function validate(str) {
      const regex = /([a-zA-Z0-9\.]){1,64}$@([a-zA-Z0-9\.]){1,64}$/;
      return regex.test(str);
    }

but it's not working. Any idea why?

2
  • 3
    Try without that $ planted in the middle Commented Jan 6, 2017 at 10:27
  • 1
    check this regex101.com/r/lFccVe/2 Commented Jan 6, 2017 at 10:30

1 Answer 1

2

You inserted the end of string anchor before the @ symbol and did not use the start of string anchor ^. Also, remove the redundant capturing groups.

Use

regex = /^[a-zA-Z0-9.]{1,64}@[a-zA-Z0-9.]{1,64}$/
         ^                 ^^
Sign up to request clarification or add additional context in comments.

2 Comments

To this question wouldn't a link to emailregex.com be the appropriate answer? ;)
@D.Kovács: No, because the requirements are different.

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.