4

Jquery can validate email addresses:
http://docs.jquery.com/Plugins/Validation

What regular expression (if any) does jQuery use for their email validation?
I'd like to do something similar, except using javascript regex.

Thanks!

update:

Why do I ask?
I'm using it for the pattern attribute of my <input> elements.

I ended up using this:

^\S+@(([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6})$

http://jsfiddle.net/Daniel_Hug/NQPhs/

1 Answer 1

8

It uses this regex borrowed from Scott Gonzalez...

/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i

Source.

There is no difference between a regex used in a jQuery plugin than a normal JavaScript regex.

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

7 Comments

Thanks! Does this seam practical for inline form validation?
@Web_Designer: Well, it's long because it validates against the RFC spec. This will guarantee the format is correct, but won't guarantee the email exists. Send an email there if you need to confirm that.
@Web_Designer - Too many sites reject my real - actual email address that is perfectly valid (it does work after all) because they use too simple of a regex.
@Web_Designer - If you want something short then how about a really permissive pattern to see if the input is at least in the ballpark. Or in the ballpark's carpark: /^[^\s].*@[^\s]+\.[^\s]+$/ - why obsess about it beyond that? Better to accept some invalid addresses than to reject valid ones because you got the regex slightly wrong, and in the end the person typing the address is responsible for what they type.
@Web_Designer pretty cool, it does work in the fiddle with my addresses. It did reject some things I know are legal - however uncommon they are - because, in essence, almost anything you can think of is legal to the left of the @ as long as it's correctly quoted/escaped. I don't even bother validating the field because I have to send a confirmation email anyway.
|

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.