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?

4
  • In addition to your original regex, you could check for the length ^.{,60}$. Commented Jan 7, 2014 at 6:43
  • why didnt just specify your input maxlength='60'? :D Commented Jan 7, 2014 at 6:45
  • @RafaEl - You mean in UI right that is already there but I want to re verify in backend. Commented Jan 7, 2014 at 6:46
  • stackoverflow.com/questions/1649435/… Commented Jan 7, 2014 at 6:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you really try it?
Yes I did using a different regex and number.
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.

Comments

0

Use a lookahead assertion:

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

Comments

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.