<!-- JavaScript:Extras code snippet 5. -->
<!-- Simple form which validates a UK postcode 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">
//<![CDATA[
function giveFocus(aForm) {
//gives focus to the text1 field
        aForm.text1.focus()
        return true
	}

// create a regular expression to check UK postcode format
var regExpressPostcode = /[a-zA-Z]{1,2}[0-9][0-9A-Za-z]? [0-9][a-zA-Z]{2}/

function checkForm(aForm) {
/* check for a valid postcode address format using the RegExp object method 'test()' */
	if (!regExpressPostcode.test(aForm.text1.value)){
        window.alert("Please enter a correctly formatted UK postcode: eg. CN23 6FR")
		//gives focus to field and selects incorrect postcode format text
        aForm.text1.focus()
        aForm.text1.select()
        return false
		}
	return true
	}
//]]>
</script>
</head>

<body onLoad="return giveFocus(document.frmFeedback)">
<form name="frmFeedback" id="frmFeedback" onsubmit="return checkForm(this)" action="goBack.html">
Please enter a correctly formatted UK postcode: eg. CN23 6FR<br />
<input type="text" name="text1" size="10" value="" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>