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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combining 2 sets of form validation 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi,

I have a form which has three sets of radio button groups all containg either "pass" or "fail" for each group at form load all radios are unchecked so that the user has to select either "pass" or "fail" for each group.

the following code checks to make sure each radio group has a selection

Code:
<script type="text/javascript">
function checkForm(f){

for (var els = f.elements, radio_groups = {}, i = els.length - 1; i > -1; --i)
if(els[i].type && els[i].type == 'radio' && !radio_groups[els[i].name])
radio_groups[els[i].name] = els[i].checked;
for (var p in radio_groups)
if (!radio_groups[p]){
alert('Please Check Either Pass Or Fail\nFor Each Pass/Fail Group !');
return false;
}
return true;
}
</script>
I then have the following for the submit button
Code:
onSubmit="return checkForm(this)"
This works OK, however I also want to check that if all the radios are selected and one or all have a "fail" then check to see if a reject qty has been entered for that group. to this end I have created the following code
Code:
<script type="text/javascript">
function checkFails(){

var errMessage = "";
var pfdim = document.forms["frmTK"].radDim;
var pfqb = document.forms["frmTK"].radQB;
var pfwa = document.forms["frmTK"].radWarp;
var dimrej = document.frmTK["txtDimRej"].value;
var bqrej = document.frmTK["txtQBRej"].value;
var warprej = document.frmTK["txtWarpRej"].value;

//Check Dimension Radio Group
for(var i = 0; i < pfdim.length; i++) {
	if(pfdim[i].checked == true) {
	statuspfdim = pfdim[i].value;
	}
}
//Check BQ Radio Group
for(var i = 0; i < pfqb.length; i++) {
	if(pfqb[i].checked == true) {
		statuspfqb = pfqb[i].value;
	}
} 
//Check Warp Radio Group
for(var i = 0; i < pfwa.length; i++) {
	if(pfwa[i].checked == true) {
		statuspfwa = pfwa[i].value;
	}
}

if (statuspfdim == "Fail" && dimrej == "") 
{
errMessage += "Dimensions = Fail, Please Enter Number Of Sheets Rejected !\n";
}
if (statuspfqb == "Fail" && bqrej == "") {
errMessage += "Board Quality = Fail, Please Enter Number Of Sheets Rejected !\n";
}
if (statuspfwa == "Fail" && warprej == "") {
errMessage += "Warp = Fail, Please Enter Number Of Sheets Rejected !\n";
}
 if (errMessage != "")
 {
 alert(errMessage);
 return false;
 }
}
</script>
this code also works OK on it's own however I would like to combine these 2 sets of code into 1 so that when the user tries to submit first it checks using checkForm(f)to make sure that either "pass" or "fail" has been selected for each radio group if not show the alert, if that check is OK then check to see if there are any fails using checkFails(), if that is OK then submit the form othwise show the appropiate message.

I really would appreciate any help as I have no idea how to implement this



Regards

Paul

 
You could chain them...
Code:
if (!radio_groups[p]){
    alert('Please Check Either Pass Or Fail\nFor Each Pass/Fail Group !');
    return false;
}
else
{
    return checkFails();
}

and in checkFails
Code:
 if (errMessage != "")
 {
     alert(errMessage);
    return false;
 }
 else
 {
    return true;
  }


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
[2thumbsup]Hi 1DMF and thanks for your speedy reply,

I have modified my code to the same as the code you sent me and it works brilliantly

thank you so much for your help [2thumbsup] [medal]

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top