Validation in html not working
I have a filter method implemnted(Django). The filter works based on some choices in drop down and text entered in a text field. Now i wanted to validate the form. I tried doing it like this: But its not getting validated. Can somebody help me to solve this?
<form id="myform" action="http://10.1.0.90:8080/filter/" method="POST" >
Filter By:
<select name="choices" onsubmit="document.forms[0].submit()" style="color: black; background-color: #BDBDBD" >
<option value="">Select A Choice开发者_运维问答 </option>
<option value="Name">Name</option>
<option value="Designation" >Designation</option>
<option value="EmployeeID" >EmployeeID</option>
<option value="Project" >Project</option>
<option value="DateOfJoin" >Date Of Join</option>
</select>
<input type="text" name="textField" style="color: black; background-color: #BDBDBD" >
<input type="submit" value="Go" onclick="validator()">
</form>
<script type="text/javascript">
function validator(){
if (document.getElementById('Designation').value == "")
{
alert("Enter a value");
}
}
</script>
I have build a demo at http://jsbin.com/ayuzi4/
Check if it helps.
The changes are
<select name="choices" onsubmit="document.forms[0].submit()" style="color: black; background-color: #BDBDBD" >
is
<select name="choices" id="choices" style="color: black; background-color: #BDBDBD" >
And
<input type="submit" value="Go" onclick="validator()">
is now
<input type="submit" value="Go" onclick="return validator();">
and the new js function is
function validator(){
if (document.getElementById('choices').value === "")
{
alert("Enter a value");
return false;
}
}
if (document.myform.getElementById('Designation').value == "") and add return false; } else { return true; }
not sure....try this
Look closely at your code and try and understand what the various Javascript functions are doing.
On the submit button, you have a function that calls the validator. But on the select box itself, you have a function that submits the form! So as soon as you click the select box, the form will be submitted, so the validator function is never called.
精彩评论