简体   繁体   中英

Javascript Page Redirect based on user input

I'm trying to figure out how to redirect user to the different URL based on text input entry.

Submit button just opens a new empty page, doesn't matter if there is a value in or not, so looks like it's not happy.

Not sure where is the issue, please advise : )

  <script type="text/javascript">
    function valForm( form ){

        var firstVal, error = '';


        if( firstVal.length == 0){
            error += 'Desk Number is required\n';
        }

         if( lastVal.length == 430){
            window.location = "http://192.168.0.4/250/";
        } 


        if( error ){
            alert( error );
            return false;
        }

        return true; 
    }
    </script>
    <h2>**Form**</h2>
    <form method="post" action="javascript" enctype="text/plain" onsubmit="return valForm(this);">
        <input type=text name="first" id="first" size="20"> Desk Number<BR>
        <input type="submit" value="Submit">
        <input type="reset" value="Clear Form"><br>
    </form>

Not sure if you were looking for a value length of 430 or a value of 430 so i changed it to value==430 for the check. This will redirect you when you type in "430" and click submit.

<form name="deskNum">
  <input type=text name="first" id="first" size="20"> Desk Number<BR>
  <input type="button" value="Submit" onclick="javascript: valForm()">
  <input type="reset" value="Clear Form">
</form>
<script>
    function valForm(){
    var firstVal=document.forms["deskNum"]["first"].value, error = '';

    if( firstVal.length == 0){
        error += 'Desk Number is required\n';
    }

     if( firstVal == 430){
        window.location = "http://192.168.0.4/250/";
    }

    if(error){
        alert(error);
    }
}
</script>

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