Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pause for validation before continue HELP!

Status
Not open for further replies.

williadn48

Programmer
Oct 27, 2006
29
US
How do I write the following code in javascript:
function DoMultipleActions(){
DoFormChecks();
If DoFormChecks = pass then keep going
WriteStartTime();
else
DoFormChecks() again
end if
ChangeBtns_frmFlds1();
StartTimer();
}

My current code (below) will not pause and let me enter required data in the form and then go back and start over at DoFormChecks. It just keeps going.
function DoMultipleActions(){
DoFormChecks();

WriteStartTime();

ChangeBtns_frmFlds1();
StartTimer();
}
function DoFormChecks()
{
var selFA = document.frmTIMETRACKING.drpeFUNCAREA.value;
var selSF = document.frmTIMETRACKING.drpeSUBFUNC.value;
var inpBatches = document.frmTIMETRACKING.veBatches.value;
var inpComments = document.frmTIMETRACKING.veCOMMENTS.value;
//If Func Area = QC and...
if (selFA == 100)
{
//If Sub Func = Auditing...
if (selSF == 120)
{
//...then a number of batches must be entered.
if (inpBatches == "")
{
alert("You must enter number of batches.");
document.frmTIMETRACKING.btnStart.disabled = false;
return false;
}
}
}
//If Func Area = QC and...
if (selFA == 100)
{
//If Sub Func = System Errors or Other...
if (selSF == 121 || selSF == 8)
{
//...then comments must be entered.
if (inpComments == "")
{
document.frmTIMETRACKING.btnStart.disabled = false;
alert("You must enter comments.");
return false;
}
else
{
return true;
}
}
}
//If Func Area = Misc. and...
if (selFA == 102)
{
//If Sub Func = Meetings or Training or Projects...
if (selSF == 124 || selSF == 126 || selSF == 127)
{
//...then comments must be entered.
if (inpComments == "")
{
alert("You must enter comments.");
document.frmTIMETRACKING.btnStart.disabled = false;
return false;
}
}
}
}
 
Just don't let the submit go through if validation fails... return false through the onsubmit property on the form:

Code:
<form action="" onsubmit="return checkValidation();">
...
</form>

The function checkValidation() is run when you click the submit button. If you return false from the function, then the submit will cancel (allowing your user to enter extra data and then try again). If you return true, then it will continue to submit.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top