简体   繁体   中英

how can i change the output of JavaScript to go to directly to another page instead of an alert window?

    <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>

I need to use this to allow the user to go either to a page that says sorry we cannot service your area or to another page that says yes we can service your area based on wheter their zipcode is listed.

also how can i add more than one zip code in the isValid = line?

<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>

Setting window.location.href = "your url here"; answers the first part of your question.

"also how can i add more than one zip code in the isValid = line?"

If you want to stick with a regex test you can use the regex or | :

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

Note that I've also added ^ and $ to match the beginning and end of the entered string - the way you had it you'd also get matches if the user entered a longer string containing that code, eg, "abc20132xyz" would match.

I'd be more inclined to do this validation server-side though.

if (isValid)
    document.location.href="validzipcode.html";
else {
    document.location.href="yep.html";
}
function IsValidZipCode(zip) {
    var isValid = /20132/.test(zip);
    if (isValid)
        document.location.href="valid_zip.html";
    else {
        document.location.href="not_valid_zip.html";
    }
}

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

window.location.href = "http://stackoverflow.com";

You can try ajax for this:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM