Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Validation Not Alerting User
    primarykey
    data
    text
    <p>I am using javascript to validate a web form for submission to a database, I have coded the form and the validation, but it does not seem to work.</p> <p>When the form is completed and it is valid it is meant to go to a success page in a separate HTML file, but if it is not valid, the text on the form turns red, a message box pops up and alerts the user that it is invalid. On Chrome and IE, it seems to display the error but go to the success page anyway. The Message Box does not come up either, i only see the text turning red before it goes to the success page.</p> <p>I wanted to know how to fix this. I have searched on the Internet but found no solution.</p> <p>I have a function called <code>validateForm()</code>, which is a series of if statements checking the if the fields are valid, and if they are not, a variable called 'result' is set to false, at the end, it sends the alert message and returns the variable 'result'.</p> <p>The code for my submit button is here:</p> <pre><code>&lt;input type="submit" name="submit" value="Submit" onclick="return validateForm();" /&gt; </code></pre> <p>The Success Page is set in the form code as an 'action'.</p> <p>Here is my Validate Form :</p> <pre><code>function validateForm(){ var result = true; var msg = ""; if(document.ExamEntry.name.value==""){ msg+="You must enter your Name \n"; document.ExamEntry.name.focus(); document.getElementById('name').style.color="red"; result = false; } if(document.ExamEntry.subject.value==""){ msg+="You must enter the Subject \n"; document.ExamEntry.subject.focus(); document.getElementById('subject').style.color="red"; result = false; } if(document.ExamEntry.examNumber.value=="") { msg+="You must enter the Examination Number \n"; document.ExamEntry.examNumber.focus; document.getElementById('examnumber').style.color="red"; result = false; } if(document.ExamEntry.examNumber.value.length!=4) { msg+="Your Examination Number must be 4 characters long \n"; document.ExamEntry.examNumber.focus; document.getElementById('examnumber').style.color="red"; result = false; } var lvlmsg=""; for(var i=0; i &lt; document.ExamEntry.level.length; i++) { if(document.ExamEntry.level[i].checked) { lvlmsg = document.ExamEntry.level[i].value; break; } } if(lvlmsg=="") { msg+="You Must Indicate Your Level"; result=false; document.getElementById('radioButtons').style.color="red"; } else { alert(lvlmsg); } if(msg==""){ return result; } else{ alert(msg); return result; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload