JavaScript Validation of Form Fields: Email Address

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

<title>JavaScript Email Address Validation</title>

<SCRIPT LANGUAGE=”JavaScript”>

function ValidateEmail(form) {

// check for invalid email address format

if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(form.email.value)){

// if no invalid characters then pass validation

alert(“The email address you have entered has been validated successfully”)

return (true)

}

// if invalid characters then fail validation

alert(“The email address you have entered is invalid, please use ‘name@yourdomain.com'”)

return (false)

}

</script>

<style type=”text/css”>

<!–

.style1 {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: small;

font-weight: bold;

}

–>

</style>

</head>

<body>

<br /><br />

<form onSubmit=”return ValidateEmail(this)” action=””>

<table border=”0″ cellspacing=”0″ cellpadding=”5″ style=”margin: auto;”>

<tr>

<td><span class=”style1″>Enter Email Address:</span> </td>

<td><input type=”text” name=”email” /></td>

<td><input type=”submit” value=”Validate” /></td>

</tr>

</table>

</form>

</body>

</html>

Validated by W3C (2010) Markup Validation Service

References

W3C (2010) Markup Validation Service [Online]. Available at http://validator.w3.org (Accessed 5 September 2010).