简体   繁体   中英

OnSubmit javascript only working once for false entries, not repeating return to form, but submiting second time false values entered?

I am new to programming and could do with some help can not find any clue, answer to this problem. Have checked javascript is ticked in Firefox 5.0.

Set up is I have a HTML page with one javascript check function at present for test development. It is linked to a servlet in tomcat.

Problem is when I enter wrong the first time, get alert message and it returns to the HTML original page.

However if I click on entry field and enter wrong again. It submits the form, does not give an alert or return to the original form?

Has any one got a solution or is this how validation should work.

Code is:

<HTML><head><title>Creation of POs</title>
<H1>Purchase Order</h1>
<script type="text/javaScript">
function check() {
check = document.forms[0].SLine1.value;
if(check=="wrong"){
alert("enter correct info");
return false;
} else {
return true; }
}
</script></head><body bgcolour="yellow">
<form method="GET" action="Distest_session30a_vs1" onSubmit="return 
check()">
various inputboxes and messages
<table>
<tr><td><input type="text" name="SLine1"></td></tr>
various other lines of inputs boxes
</table>
<Input type="submit" value="Send now">
</form></body></html>

If you don't want the form to be submitted, on the form, you can use onsubmit="javascript:your_validation_function()" . The form will only be submitted if the your_validation_function() returns true.

EDIT _ Looking at your HTML and js code together, I have found your problem. This js code should work:

function check(){ 
  var check = document.forms[0].SLine1.value; 
  if(check=="wrong"){ 
    alert("enter correct info"); 
    return false; 
  } 
  else{ 
    return true; 
  } 
} 

Your problem was that you forgot to correctly declare the check var inside the function. If you are going to use variables, you need to use var .

Is this what you are trying to do?.

          <script type="text/javascript">

              function check() {
                                    check = document.forms[0].SLine1.value;
                                    if(check=="wrong")
                                     { 
                                       alert("enter correct info"); 
                                        return false;
                                     } 
                                     else if("right")
                                     {
                                       alert("right");
                                       return true; 
                                     }

                                } 
           </script>

          <form onsubmit="check()">
            <input type="text" id="SLine1">
                <input  type="submit">
         </form>

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