Mohit_t
asked on
Javascript Form Validation
Hi,
I have a javascript for doing some form validation on form submit. when I submit the form for the first time it does all the form validation and pops up an alert, and comes back on the same form, however if I click on the submit form again nothing happens, the form does not submit, unless I reload the form. Below is the JS code, appreciate if someone can help me figure out what is wrong, I have to call location.reload to make this work, which refreshes the page and the user has to enter all the data again
I have a javascript for doing some form validation on form submit. when I submit the form for the first time it does all the form validation and pops up an alert, and comes back on the same form, however if I click on the submit form again nothing happens, the form does not submit, unless I reload the form. Below is the JS code, appreciate if someone can help me figure out what is wrong, I have to call location.reload to make this work, which refreshes the page and the user has to enter all the data again
function validateForm(){
var rptdPos = document.getElementById('input_21').value;
var NPos = document.getElementById('input_20').value;
var bScore = document.getElementById('input_24').value;
var bSupport = document.getElementById('input_25').value;
var excludeInfo = document.getElementById('input_62').value;
var exReason = document.getElementById('input_63').value;
if(excludeInfo == 'Y' && exReason == ''){
alert("Exclusion reason is required to be entered");
location.reload();
return false;
}
if(excludeInfo == 'N'){
if((resinPlace == 'Y') &&((resType0 == '' && resType1 == '' && resType2 == '') || (resJustification0 == '' & resJustification1 == '' && resJustification2 == '' && resJustification3 == '' && resJustification4 == '') || resCat == '')){
alert("Please enter values for Type,Category and Justfy");
location.reload();
return false;
}
if((bScore == '1' || bScore == '2' || bScore == '3') && bSupport == ''){
alert("Please enter a value for BSupport");
location.reload();
return false;
}
}
return true;
//alert("reached end of javascript");
}
Calling the above javascript in my php form as below<form class="Cform" action="submitPatientInfo.php" onSubmit="return validateForm()" method="post" name="form_31" id="31" accept-charset="utf-8">
Also I have two buttons in the form Submit and Cancel, the javascript validation is called even if I hit the Cancel button, I want to call the validation JS only for Submit button and not Cancel button, the submit buttons are<input type="submit" value="Submit Survey for Patient" name="submitPatient" class="form-submit-button form-submit-button-blue" />
<input type="submit" id = "input_2013" value="Cancel - Do not save" name="Return" class="form-submit-button form-submit-button-simple_red" />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.