Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form validation help

Status
Not open for further replies.

flistea

Technical User
Apr 15, 2005
13
GB
I'm creating a form where users enter their address (not all shown on the code) and their postcode. If it is a UK postcode I need to make sure it conforms to UK postcode standards, so I have a series of regular expressions. Users are asked to check a radio button to say whether their address is uk or other.

At the moment I've got the code so it will always be validated against the regular expressions, but I need it to only do this if the UK option is checked. I have tried to do this but am failing badly!

Can anyone help me?

Thank you in advance!!

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Validation</title>
<script language="javascript" type="text/javascript">
<!-- Hide script from older browsers

re1 = /^([^qvxQVX][^ijzIJZ]?\d\d?)[\.\-\/\s]?(\d[^cikmovCIKMOV]{2})$/
re2 = /^([^qvxQVX]\d[abehmnprvwxyABEHMNPRVWXY])[\.\-\/\s]?(\d[^cikmovCIKMOV]{2})$/
re3 = /^([^qvxQVX][^ijzIJZ]\d[abcdefghjkstuwABCDEFGHJKSTUW])[\.\-\/\s]?(\d[^cikmovCIKMOV]{2})$/
re4 = /^([gG][iI][rR])[\.\-\/\s]?(0[aA]{2})$/


	
function submitIt(myForm) {

countryOption = -1
	for (i=0; i<myForm.country.length; i++) {
		if (myForm.country[i].checked){
			countryOption = i
			}
		}
		if (countryOption == -1) {
			alert("Please select UK or other")
			return false
			}
		
if (validPCode = re1.exec(myForm.pCode.value)){
	myForm.pCode.value = validPCode[1] + " " + validPCode[2]
}
else if (validPCode = re2.exec(myForm.pCode.value)) {
	myForm.pCode.value = validPCode[1] + " " + validPCode[2]
}
else if (validPCode = re3.exec(myForm.pCode.value)) {
	myForm.pCode.value = validPCode[1] + " " + validPCode[2]
}
else if (validPCode = re4.exec(myForm.pCode.value)){
	myForm.pCode.value = validPCode[1] + " " + validPCode[2]
}
else{
	alert(myForm.pCode.value + " is an invalid UK postcode")
	myForm.pCode.focus()
	myForm.pCode.select()
	return false
	}
	
}

	
	// End hiding script -->
	</script>
</head>
<h2 align="center">Address Validation</h2>
<form onsubmit="return submitIt(this)" action="someAction.cgi" name="myForm">
<table border="0" cellspacing="8" cellpadding="8">
<tr>
<td align="right" valign="top">
Postcode <input name="pCode" type="text" size="25" /> Country: <input type="radio" value="UK" name="country" />UK <input type="radio" value="other" name="country" />Other
</td>
<td>
<p><input type="reset"/>&nbsp;<input type="submit" value="Submit" /></p>
</td>
</tr>
	</table>
</form>
<body>
</body>
</html>
 
You can do it a number of ways.
Currently you loop through ALL of the related radio boxes and if none of them are checked you return an error but if ANY of them are checked you continue to the rest of the function.

You could just put an if statement wrapping your validation routines saying

if (countryOption == 0) { //Then UK was selected
all your validation routines in here

}

If no option was selected then your first test already returns the error, otherwise you return a value of 0 for UK or 1 for Other so it is as simple as testing for 0 to know that UK was selected.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Thanks theniteowl. [medal] I knew it had to be something stupidly simple! I had been trying to test it using (myForm.country.value == "UK") but it wasn't working.

 
That's what happens when you try different approaches, you sometimes end up forgetting which one your currently on and it takes a while to puzzle out why something is not working because you KNOW the code was good when you put it in.
:)


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top