...

View Full Version : Need help figuring out a simple zip code form



eBi
01-20-2012, 12:44 AM
Hello,

What I need is a simple service area, zip code validation form that redirects to a certain URL when a valid zip code is submitted and a different URL when an invalid zip code is submitted.

I found a form script example that works well with only a single zip code. My problem is I can't figure out how to modify it so that multiple zip codes are valid.

Here is the head part. 60016 is one of about 50 valid zip codes I need the form to accept as valid -

[code]
<script>

var correctCode = "60016";


function validateCode()
{
var code = document.getElementById("codeTextBox").value;

if (code == correctCode)
{
window.location.href = "/ggc/test1";
}
else
{
window.location.href = "/ggc/test2";
}
}
</script>
[code]

Here is the body part -

[code]
Please enter your zip code:
<input type="text" name="codeTextBox" id="codeTextBox" />
<input type="submit" name="Submit" value="Submit" onclick="validateCode()" />
[code]

Old Pedant
01-20-2012, 01:32 AM
Several ways to do it. But let's use simple brute force way.


var correctCodes = [ 60016, 60017, 60020, 60021 ]; // as many as you want in the array

function validateCode()
{
var code = Number(document.getElementById("codeTextBox").value);
for ( var c = 0; c < correctCodes.length; ++c )
{
if (code == correctCodes[c])
{
window.location.href = "/ggc/test1";
return;
}
}
window.location.href = "/ggc/test2";
}

Shouldn't really need the return line, but it won't hurt.

eBi
01-20-2012, 01:59 AM
Thanks. That did the trick. If you have any suggestions for a better or even easier way to do it then I would love to hear about it.

eBi
01-20-2012, 02:18 AM
Here's another couple of questions regarding the same form.

Can anything be added to the code so that the mouse cursor is set by default in the form field when the page loads?

Can anything be added to the code so that the Enter button on the keyboard will submit the form instead of the visitor having to use the mouse to click the submit button?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum