David
David

Reputation: 3614

regex to validate in javascript

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?

Upvotes: 1

Views: 41

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627400

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}$/
         ^                 ^^

Upvotes: 2

Related Questions