rustyuk
07-16-2008, 12:30 PM
I'm a bit of a beginner with Javascript and need some help with some code.
It is concerning a form that has a Postcode field on it. I want to include some javascript that will recognise when anybody enters a postcode from a certain area (postcodes beginning with DN1-DN11, DN15 and DN17). All that I need is for a pop up box to appear when one of these postcodes is entered by the user. It will say something along the lines of "You have been entered into a special prize draw".
This is what I have so far - it is simply code that checks the postcode is in the right format. Can anybody help me with my problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Postcode form</TITLE>
<META http-equiv=content-type content="text/html; charset=iso-8859-1"><LINK
href="site.css" type=text/css
rel=stylesheet>
<SCRIPT src="navigation.js"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
<!-- // hiding from older browsers
function validateall(){
if (document.getElementById('errorOnEntry').innerHTML==""){
return true
}
else{
alert("Please ensure all data is correct before submitting your form.")
return false
}
}
function verifypostcode(postcode,labelID){
re = /^([a-z]{2})(\d{1,2})(\ )(\d{1,2})([a-z]{2})$/i
pcodetotest = postcode.value
if (re.test(pcodetotest)){
re.exec(pcodetotest)
formattedpcode = RegExp.$1.toUpperCase() + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5.toUpperCase()
postcode.value=formattedpcode;
document.getElementById(labelID).style.color="black";
document.getElementById('errorOnEntry').innerHTML=""
}
else{
alert ("Not a valid post code")
document.getElementById(labelID).style.color="red";
document.getElementById('errorOnEntry').innerHTML="<br> Red fields indicate an<br>error with the data entered.";
}
}
// -->
</SCRIPT>
<META content="MSHTML 6.00.2900.5512" name=GENERATOR></HEAD>
<BODY><!-- Page-wide header -->
<TR>
<TD><LABEL id=pcodelabel for=shipPostalCode>Postal Code</LABEL></TD>
<TD><INPUT id=shipPostalCode maxLength=30
onchange="verifypostcode(this,'pcodelabel')" name=shipPostalCode></TD></TR>
<TR>
<TD><INPUT id=Submit onClick="return validateall()" type=submit value=Submit name=Submit></TD>
<TD> </TD></TR></TBODY></TABLE></FORM></DIV><!-- Rightmost column contents -->
</BODY></HTML>
It is concerning a form that has a Postcode field on it. I want to include some javascript that will recognise when anybody enters a postcode from a certain area (postcodes beginning with DN1-DN11, DN15 and DN17). All that I need is for a pop up box to appear when one of these postcodes is entered by the user. It will say something along the lines of "You have been entered into a special prize draw".
This is what I have so far - it is simply code that checks the postcode is in the right format. Can anybody help me with my problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Postcode form</TITLE>
<META http-equiv=content-type content="text/html; charset=iso-8859-1"><LINK
href="site.css" type=text/css
rel=stylesheet>
<SCRIPT src="navigation.js"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
<!-- // hiding from older browsers
function validateall(){
if (document.getElementById('errorOnEntry').innerHTML==""){
return true
}
else{
alert("Please ensure all data is correct before submitting your form.")
return false
}
}
function verifypostcode(postcode,labelID){
re = /^([a-z]{2})(\d{1,2})(\ )(\d{1,2})([a-z]{2})$/i
pcodetotest = postcode.value
if (re.test(pcodetotest)){
re.exec(pcodetotest)
formattedpcode = RegExp.$1.toUpperCase() + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5.toUpperCase()
postcode.value=formattedpcode;
document.getElementById(labelID).style.color="black";
document.getElementById('errorOnEntry').innerHTML=""
}
else{
alert ("Not a valid post code")
document.getElementById(labelID).style.color="red";
document.getElementById('errorOnEntry').innerHTML="<br> Red fields indicate an<br>error with the data entered.";
}
}
// -->
</SCRIPT>
<META content="MSHTML 6.00.2900.5512" name=GENERATOR></HEAD>
<BODY><!-- Page-wide header -->
<TR>
<TD><LABEL id=pcodelabel for=shipPostalCode>Postal Code</LABEL></TD>
<TD><INPUT id=shipPostalCode maxLength=30
onchange="verifypostcode(this,'pcodelabel')" name=shipPostalCode></TD></TR>
<TR>
<TD><INPUT id=Submit onClick="return validateall()" type=submit value=Submit name=Submit></TD>
<TD> </TD></TR></TBODY></TABLE></FORM></DIV><!-- Rightmost column contents -->
</BODY></HTML>