0
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Validate Zip Code</title>
            <script type="text/javascript">
                function IsValidZipCode(zip) {
                    var isValid = /20132/.test(zip);
                    if (isValid)
                        alert('Valid ZipCode');
                    else {
                        alert('yep')
                         }
                }
            </script>
        </head>


   <body>
    <form>
    <input id="txtZip" name="zip" type="text" /><br />
    <input id="Button1" type="submit" value="Check My Zipcode"
    onclick="IsValidZipCode(this.form.zip.value)" />
    </form>
    </body>
    </html>

我需要使用它来允许用户转到表示抱歉我们无法为您所在地区提供服务的页面或另一个表示我们可以根据他们的邮政编码为您所在地区提供服务的页面。

另外,如何在 isValid = 行中添加多个邮政编码?

4

6 回答 6

1
if (isValid)
    document.location.href="validzipcode.html";
else {
    document.location.href="yep.html";
}
于 2012-11-02T05:40:28.700 回答
1

设置window.location.href = "your url here";回答了问题的第一部分。

“我如何在 isValid = 行中添加多个邮政编码?”

如果您想坚持使用正则表达式测试,您可以使用正则表达式或|

var isValid = /^(20132|20133|20200|90210|etc)$/.test(zip);

请注意,我还添加了^and$以匹配输入字符串的开头和结尾 - 如果用户输入包含该代码的更长字符串(例如,"abc20132xyz"将匹配),您也会得到匹配项。

不过,我更倾向于在服务器端进行此验证。

于 2012-11-02T05:33:47.413 回答
1
<script type="text/javascript">
            function IsValidZipCode(zip) {
                var isValid = /20132/.test(zip);
                if (isValid)
                  window.location = baseurl."/valid.php"
                else {
                    window.location = baseurl."/invalid.php"
                     }
            }
</script>
于 2012-11-02T05:24:17.873 回答
0
function IsValidZipCode(zip) {
    var isValid = /20132/.test(zip);
    if (isValid)
        document.location.href="valid_zip.html";
    else {
        document.location.href="not_valid_zip.html";
    }
}
于 2012-11-02T05:22:45.973 回答
0

您可以使用以下代码在 javascript 中重定向它,可​​以将其放在您的示例中,而不是 alert()

window.location.href = "http://stackoverflow.com";
于 2012-11-02T05:25:03.080 回答
0

您可以为此尝试ajax:

<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Validate Zip Code</title>



<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function IsValidZipCode(zip){

    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

        //alert(ajaxRequest);
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){


        if(ajaxRequest.readyState == 4){
            //var ajaxDisplay = document.getElementById('ajaxDiv');
            //ajaxDisplay.innerHTML = ajaxRequest.responseText;
                        alert(ajaxRequest.responseText);
                        if(ajaxRequest.responseText=="")
                        {
                                alert("sorry we cannot service your area");
                        }
                        else{
                                window.location = "Page.php?zip="+zip+"";
                        }

        }
    }
    //var age = document.getElementById('age').value;
    //var wpm = document.getElementById('wpm').value;
    //var sex = document.getElementById('sex').value;

    var queryString = "?zip=" + zip;

    ajaxRequest.open("GET", "zip_check.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



        </head>


   <body>
    <form name="form1" method="post">
    <input id="txtZip" name="zip" type="text" /><br />
    <input type="button" id="Button1"  value="Check My Zipcode"
    onclick="IsValidZipCode(document.form1.zip.value)" />
    </form>
    </body>
    </html>
于 2012-11-02T05:47:22.783 回答