4
I found the following regular expression to validate javascript email address:
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Which works really well. I would like to add, in the same expression, the minimum and maximum character limitations:
/^.{3,100}$/
A less elegant solution is to do 2 checks (which is what I’m doing today). It is possible to leave everything in a single expression?
I don’t know if you noticed, but your regex already measures the size of specific fragments of your expression. You can see, by the way, that there are several
{1,3}
there in the middle that make all the difference. You can know more by testing your expressions and rules using an online Bugger, such as the Regexpal.– Guilherme Oderdenge
It is necessary to be very careful with the validation of email, in my opinion it is convenient to make it as simple as possible, since there is a huge variety of possible emails. I do a very simple validation to see if there is:
[email protected]
, that’s all.– Jorge B.
@Jorgeb. Although I agree in principle that making it simple is good, one must be careful not to have false negatives. For example, I’ve been using the formula
[email protected]
to prevent spam (and to find out which website I registered for sold my email to spammers), but many sites rejected it because of+
at the address - which is something perfectly valid.– mgibsonbr
@mgibsonbr when I say
texto@
I meant any valid character.– Jorge B.
Advice: Use the Jquery validate for the validation.
– Jorge B.
@mgibsonbr Are they "false positives"? Haha.
– Guilherme Oderdenge
I wouldn’t recommend that. Unless you have thought about ALL possible users' over-stupidity and are protected against ALL the possibilities of this ER failing (which is extremely difficult in most scenarios), sooner or later this ER will need maintenance, if you support yourself or someone else. And let’s come and agree that reading a complex ER is not so easy.
– Bruno Augusto
@Brunoaugusto If the logic of the OP depends on capture groups within this regex, then I agree with your argument. Otherwise, would it not simply be the case to replace this regex with another case if it needs maintenance? Validating emails is a common task, and there are initiatives to find good regexes that are actually compatible with the specification (example). It is a complex task, and ad-hoc solutions have great chance to contain IMHO errors.
– mgibsonbr
Yes, but a very large ER makes it difficult to even rewrite a new revision of it because invariably the programmer will have to read and interpret it. I don’t know if JS has ER support multiline like PHP where you can comment on each segment of it.
– Bruno Augusto
Mercy, what a catastrophe was the wording of my penultimate comment.
– Bruno Augusto
The answer is correct but I agree with the comments and benefits of a simple validation since it is a standard that I do not have full control and can change regardless of my business rules.
– Onaiggac