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);
}
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.