1

Please find below the regex I am using for validating an email address. This is working fine.

^[-!#$%&\\'*+\\\\./<MORE_REGEX_HERE>^_`a-z{|}~]+$

Now I want to add a length check in this regex for example the email address can be of max length 60. So I tried something like below

^([-!#$%&\\'*+\\\\./<MORE_REGEX_HERE>^_`a-z{|}~]+){1,60}$

But its not working. Any thoughts?

A Paul
  • 8,113
  • 3
  • 31
  • 61

3 Answers3

2

Hey you can find the below solution if it works:-

It might be so that you are having multiple matching strings within a single -

^[-!#$%&\'*+\\.]+[^_`a-z{|}~]+$

in such cases you cannot use ^[-!#$%&\'*+\\.]+[^_`a-z{|}~]{1,60}$

to match the whole string but it matches only the second part of the string in such cases you need to use the below regex: -

[^[-!#$%&\\'*+\\\\.]+[<MORE_REGEX_HERE>^_`a-z{|}~]+$]{1,60}

Try it should work now.

user3115056
  • 1,266
  • 1
  • 10
  • 25
0

If you want a valid way of validating emails try this: Good solution

Using {n,m} matches n(minimum inclusive) to m (maximum inclusive) characters.

Community
  • 1
  • 1
AlfredoVR
  • 4,069
  • 3
  • 25
  • 33
0

Use a lookahead assertion:

^(?=[-!#$%&\\'*+\\\\./<MORE_REGEX_HERE>^_`a-z{|}~]+).{1,60}$
Toto
  • 89,455
  • 62
  • 89
  • 125