<!-- JavaScript:Forms code snippet 7. -->
<!-- Simple form which validates an email address format
using a 'regular expression' -->
<!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" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
<script language="JavaScript" type="text/javascript">
function giveFocus(aForm) {
//gives focus to the text1 field
aForm.text1.focus()
return true
}
// create a regular expression to check email format
var regExpressEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
function checkForm(aForm) {
/* check for a valid email address format using the RegExp object method 'test()' */
if (!regExpressEmail.test(aForm.text1.value)){
window.alert("Please enter a correctly formatted email address: eg. me@me.com")
//gives focus to field and selects incorrect email format text
aForm.text1.focus()
aForm.text1.select()
return false
}
return true
}
</script>
</head>
<!-- note that the 'body' tag contains the 'onLoad' attribute. -->
<!-- call the function giveFocus() when the page has finished loading -->
<body onLoad="return giveFocus(document.frmFeedback)">
<form name="frmFeedback" id="frmFeedback" onsubmit="return checkForm(this)" action="6_testLink2.php">
Please enter a correctly formatted email address: eg. me@me.com<br />
<input type="text" name="text1" size="100" value="" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>