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);
} 
Trey Copeland
  • 3,387
  • 7
  • 29
  • 46
  • 3
    Have you searched a bit on Google? – talnicolas Jan 27 '12 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. – lincolnk Jan 27 '12 at 20:24
  • 1
    Have you searched a bit on Stackoverflow? http://stackoverflow.com/search?q=validate+email+regex&submit=search – Don Roby Jan 27 '12 at 20:25
  • 1
    possible duplicate of [Validate email address in Javascript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Don Roby Jan 27 '12 at 20:27
  • Here's an example of a quite complete regex for email btw: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – talnicolas Jan 27 '12 at 20:45

4 Answers4

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.

Community
  • 1
  • 1
Robin Orheden
  • 2,714
  • 23
  • 24
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.

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

Try RegExpLib Site Reference

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

Tim
  • 4,051
  • 10
  • 36
  • 60
0

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

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662