简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM