Forum Moderators: open
Please tell me how I can check on submit click if any text field is empty before it goes to complited.php with jquery. I want to check on click if any name is empty. If it is, than var checking=1 else continue with complited.php.
Thank you
<form action="complited.php" method="post" onsubmit="validate();">
<input type="text" name="test" id="test" />
<span id='testVal'></span>
<input type="text" name="test2" id="test2" />
<span id='test2Val'></span>
<input type="submit" />
</form>
<script>
function validate() {
var isGood = true;
//validate test
if(document.getElementById('test').value=='') {
isGood = false;
document.getElementById('testVal').InnerHTML = 'required';
}
//validate test2
if(document.getElementById('test2').value=="") {
isGood = false;
document.getElementById('test2Val').InnerHTML = 'required';
}
//return true or false
return isGood;
}
$("#idOfYourForm").submit(function () {
if (document.getElementById('test').value=='') {
alert("Please provide a test value");
return false;
} else if (document.getElementById('test2').value=="") {
alert("Please provide a test value");
return false;
}
return true;
});