0

I'm using the following code to make sure fields are filled in. How can I add some code to make sure it's a valid email?

function verify() { if ($("#ddlBusinessName").val() == "0") { alert("Please select the name."); } else if ($("#txtEmail").val().length <= 0 || $("#txtEmailConfirm").val().length <= 0) { alert("Please enter the email address and confirm the email address."); else if ($("#TxtPassword").val().length <=0 || $("#TxtConfirmPassword").val().length <=0) { alert("Please enter your password."); } } 

EDIT How can I implement this into my code though?

 function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\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,}))$/; return re.test(email); } 
5
  • 3
    Have you searched a bit on Google? Commented Jan 27, 2012 at 20:22
  • you are never going to fully validate an email address with regular expressions. find a library that will validate it for you. Commented Jan 27, 2012 at 20:24
  • 1
    Have you searched a bit on Stackoverflow? stackoverflow.com/… Commented Jan 27, 2012 at 20:25
  • 1
    possible duplicate of Validate email address in Javascript? Commented Jan 27, 2012 at 20:27
  • Here's an example of a quite complete regex for email btw: ex-parrot.com/~pdw/Mail-RFC822-Address.html Commented Jan 27, 2012 at 20:45

4 Answers 4

1

The only safe way to validate an email address is actually sending an email to it. If this fails with a (permanent - graylisting errors would be temporary) error code you know the address is invalid.

Of course you cannot do that in JavaScript - so I'd limit the client-side check to a very simple check - something like /^[^@ ]+@[^@ ]+\.[^@ ]+$/ (thx @NedBatchelder) would do the job.

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

Comments

1

I just answered a similar question. https://stackoverflow.com/a/12674524/340736

For your client-side validation I would recommend that you use Verimail.js. It validates your e-mail address against a RFC 822 regular expression, but also a full list of all registered TLD's.

Comments

0

Try RegExpLib Site Reference

Note that you're validating the email could exist, not that it does exist!

Comments

0

Don't try to get too fancy with email address validation. Try this: Humane email validation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.