Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    eBi
    eBi is offline
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help figuring out a simple zip code form

    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]

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Several ways to do it. But let's use simple brute force way.
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    eBi
    eBi is offline
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    eBi
    eBi is offline
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •