PDA

View Full Version : Run JavaScript function after validation



4fit?
10-10-2006, 12:51 PM
I have a JavaScript validation function that checks to make sure that each combobox has had a selection made in it. Here is that code:

function checkform() {
var TWIndex=document.TireDiameterForm.TireWidth.selectedIndex;
var SWIndex=document.TireDiameterForm.SideWall.selectedIndex;
var WDIndex=document.TireDiameterForm.WheelDiameter.selectedIndex;
if (TWIndex==0){
alert('There is a problem with the Tire Width field');
return false;}
else if (SWIndex==0) {
alert('There is a problem with the Sidewall field');
return false;}
else if (WDIndex==0) {
alert('There is a problem with the Wheel Diameter field');
return false;}
return true;
}

If this function returns true, I want this additional function to run. How do I do this? Thanks in advance.

function TireDiameter() {
var mm=.0393701
var w = document.TireDiameterForm.TireWidth.value;
var s = document.TireDiameterForm.SideWall.value;
var d = document.TireDiameterForm.WheelDiameter.value;
var y = Math.round((((((w * s) / 100) * 2) * mm) + (d * 1)) * 100) / 100;
document.TireDiameterForm.Result.value = y;}

codeexploiter
10-10-2006, 01:02 PM
function checkform() {
var TWIndex=document.TireDiameterForm.TireWidth.selectedIndex;
var SWIndex=document.TireDiameterForm.SideWall.selectedIndex;
var WDIndex=document.TireDiameterForm.WheelDiameter.selectedIndex;
if (TWIndex==0){
alert('There is a problem with the Tire Width field');
return false;}
else if (SWIndex==0) {
alert('There is a problem with the Sidewall field');
return false;}
else if (WDIndex==0) {
alert('There is a problem with the Wheel Diameter field');
return false;}
TireDiameter(); //Calling the function just before you are going to return a true value from the validation function.
return true;
}


I think you just want to make sure that before making a call to TireDiameter() that your form validation was successful.

If you look at the above code you'll reach the TireDiameter(); only if you are going to return a true and i think this will give you the same effect you wants after that function call it will return true. Here it won't make much difference whether you call the function before or after that return because you will call the function only if you are going to return a true value.

If you are returning a false value you'll never reach the function calling step.

4fit?
10-10-2006, 01:08 PM
Perfect. Thanks alot codeexploiter. This is my first attempt at JavaScripting so I still have LOADS to learn. Thanks again!:D