StuffandBlah
StuffandBlah

Reputation: 1067

Email regex with character limit (without lookahead)

I have a regex: "\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}\b" for validating email addresses within ASP.NET.

I would like to be able to limit the overall number of characters between 6 & 100 characters. I'm coding for IE 6, so how can this be done without using a lookahead?

Many thanks

Upvotes: 0

Views: 646

Answers (2)

Brad
Brad

Reputation: 15577

Give this a spin:

This requires the username to be between 6 and 600 characters. Without knowing how long the domain may be, I don't think you're going to be able to limit it effectively without a lookahead (or behind). The only possible workaround I can think of would be to set arbitrary limits on each: say 300 for the username, and 300 for the domain.

^(#?[_a-zA-Z0-9+-](\.?[_a-zA-Z0-9+-]{5,599})*)@([a-zA-Z0-9]+(-(?!-)|[a-zA-Z0-9\.])*?[a-zA-Z0-9]+\.([0-9]{1,3}|[a-zA-Z]{2,3}|(aero|arpa|asia|coop|info|jobs|mobi|museum|name|travel)))$

Upvotes: 1

Paul Butcher
Paul Butcher

Reputation: 6956

Why not just check the length separately, instead of trying to shoehorn it into the regex?

Upvotes: 1

Related Questions