2

I have a regex for email validation, what I wanna do is limit the characters for the domain part, I know that I will use this {1,10} but I don't know where should I put it. Thank you

/(?!.*\.{2})^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0]|[a-z\d\u00A0][a-z\d\-._~\u00A0]*[a-z\d\u00A0])\.)+([a-z\u00A0]|[a-z\u00A0][a-z\d\-._~\u00A0]*[a-z\u00A0])\.?$/i
Nyaa NyaaA
  • 25
  • 4
  • By domain, do you mean everything after the `@`? So the total length of everything after that should be between 1 and 10 inclusive? – Chase Jun 11 '20 at 06:06
  • This question is by no means a duplicate of [How to validate an email address in JavaScript](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) since the regex is already doing what tha thread deals with. The problem is adding an extra requirement for the domain part of the email here. – Wiktor Stribiżew Aug 13 '20 at 08:40

1 Answers1

0

Add (?![^@]{11}) after ^ to disallow 11 characters other than @ at the start of string:

(?!.*\.{2})^(?![^@]{11})([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0]|[a-z\d\u00A0][a-z\d\-._~\u00A0]*[a-z\d\u00A0])\.)+([a-z\u00A0]|[a-z\u00A0][a-z\d\-._~\u00A0]*[a-z\u00A0])\.?$

See proof

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
  • 1
    yes, Thank you for this. I have limited the domain and local part in regex using this Yes! thank you for this. I have also fixed this, limiting the domain and local part using this: `/(?!.*\.{2})^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]{1,65}(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\-\u00A0]|[a-z\d\u00A0][a-z\d\-._~\u00A0][a-z\d\-\u00A0]{0,253})\.)+([a-z\u00A0]|[a-z\u00A0][a-z\d\-._~\u00A0]*[a-z\u00A0])\.?$/i` – Nyaa NyaaA Jun 15 '20 at 02:15