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

How to call a function from within a function, etc.? :-) 4

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
I have the two functions (as shown below).

So, my question is ...

What is the best way to have a FORM check both of these before processing the information to a perl script.

Can I have the onSubmit call both of the functions (one after the other) before it submits the information to a perl script ... and if so, ... how?

Or, ...

Can I combine the two together somehow?

Or, ...

Can I have one function ... call the other function (when it reached the end of the first one) ... to kind of link multiple functions together ... one after the other?

I could really use some good advice and help.

Thanks.
Gary

============

CODE:

<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
<!--

function validateCheckBoxes(checkboxName, checkboxList) {

var checkboxValue = null;
for (var i=0; i < checkboxList.length; i++) {
if (checkboxList.checked) {
checkboxValue = checkboxList.value;
break;
}
}
if (checkboxValue == null) {
alert(&quot;Please be sure to check off a minimum of at least ONE checkbox.&quot;);
return false;
}
/*
else {
alert(&quot;Thanks for selecting our &quot; + checkboxValue + &quot; &quot; + checkboxName + &quot; Netwood Design Center boxer shorts. We will mail them to your grandmother immediately.&quot;);
return false; // in operation would be &quot;return true&quot;
}
*/
}

//-->
</script>


<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
<!--

function checkData()

{

var correct = true;

if (document.Form.first_name.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your first name in the field titled: 'First Name'&quot;)}

if (document.Form.last_name.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your last name in the field titled: 'Last Name'.&quot;)}

if (document.Form.email.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your e-mail address in the field titled: 'E-mail Address'.&quot;)}

return correct;

}

//-->

</script>

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Just create another function called something like &quot;validateAll&quot; and in it, just have the 2 calls and call this new function from the OnSubmit.

So

function validateAll(blah...) {
return (validateCheckBoxes(blah,blah) && checkData());
}

Yeah? and OnSubmit=&quot;JavaScript:validateAll(blah...);&quot;

That simple?


 
You can call them both in the onSubmit action, just seperate the functions with a semi-colon (;)

onSubmit=&quot;validateCheckBoxes(checkboxName, checkboxList); checkData()&quot; - tleish
 
Please don't think I'm stupid, ... but

From what you saw ..

What would I need to put for the 'blah' ... as in:

function validateAll(blah...) {

I guess I was just a little confused.

Could I just have:

function validateAll() {
return (validateCheckBoxes(checkboxName, checkboxList) && checkData());
}


Or ... do I need to put something in the &quot;validateAll&quot; parenthesis?

Sorry again for my lack of knowledge on this.

But THANK YOU.

Gary

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
no worries... go for the second suggestion - put a semi colon in - I always overcomplicate everything!! - listen to tleish. (and blah was just because I couldn't be bothered to type in the parameters you were using). Cheers.
 
tleish,

When I use:

onSubmit=&quot;validateCheckBoxes(checkboxName, checkboxList); checkData()&quot;

It notifies me of any fields that didn't have information within them ... but it simply sends the information on - regardless. It doesn't stop. It just continues to send the form through its action.

How do I get it to 'check' and validate the form .. and not continue on ... unless all conditions are met?

Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
try putting a return in, so

onSubmit=&quot;return (validateCheckBoxes(checkboxName, checkboxList) && checkData());&quot;

worth a try don't you think?
 
That did'nt seem to do it either.

Here's the full script. (So you can see what I am working with including the html form and submit information.)


<!doctype html public &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title> Validation Form - for Checkboxes, Radio Buttons, and Text Fields </title>

<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
<!--

function validateCheckBoxes(checkboxName, checkboxList) {

var checkboxValue = null;
for (var i=0; i < checkboxList.length; i++) {
if (checkboxList.checked) {
checkboxValue = checkboxList.value;
break;
}
}
if (checkboxValue == null) {
alert(&quot;Please be sure to check off a minimum of at least ONE checkbox to submit this form.&quot;);
return false;
}
}

//-->
</script>


<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
<!--

function checkData()

{

var correct = true;

if (document.Form.first_name.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your first name in the field titled: 'First Name'&quot;)}

if (document.Form.last_name.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your last name in the field titled: 'Last Name'.&quot;)}

if (document.Form.email.value == &quot;&quot;) {correct = false; alert(&quot;Please enter your e-mail address in the field titled: 'E-mail Address'.&quot;)}

return correct;

}

//-->

</script>


<style type=&quot;text/css&quot;>
<!--

TD {
font-family: arial;
font-size: 10pt;
}

//-->
</style>

</head>

<body>

<font face=&quot;arial&quot; size=&quot;2&quot;>
This form will <b>first</b> check the text fields ... and return a message for each one that wasn't completed.<br>
<br>
<b>Next</b>, it will validate the 'checkboxes' and provide a separate reminder indicating that the user needs to check off at least ONE of the checkboxes in order
to submit the form.<br>
<br>

<form name=&quot;Form&quot; method=&quot;post&quot; action=&quot;/cgi-bin/FILENAME.cgi&quot; onSubmit=&quot;return validateCheckBoxes('size', this.size)&quot;>

<table border=&quot;0&quot;>
<tr>
<td valign=&quot;top&quot;><input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;small&quot;> Small<br>
<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;medium&quot;> Medium<br>
<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;large&quot;> Large<br>
<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;extra_large&quot;> Extra Large<br>
</td>
</tr>
</table>

<br>
First Name: <input type=&quot;text&quot; name=&quot;first_name&quot; size=&quot;10&quot; maxlength=&quot;25&quot;> &nbsp; Last Name: <input type=&quot;text&quot; name=&quot;last_name&quot; size=&quot;15&quot; maxlength=&quot;35&quot;><br>
<br>
Email Address: <input type=&quot;text&quot; name=&quot;email&quot; size=&quot;25&quot; maxlength=&quot;50&quot;><br>
<br>
<div align=&quot;center&quot;>
<input type=&quot;submit&quot; value=&quot;Submit&quot; onClick=&quot;return checkData()&quot;> <input type=&quot;reset&quot; value=&quot;Reset&quot;>
</div>


</form>


</font>

</body>
</html>



Please let me know what you think.
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Try this:

[COLOR=000080]<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>[/color]

[COLOR=000080]<HTML>[/color]
[COLOR=000080]<HEAD>[/color]
[COLOR=000080]<TITLE>[/color]Validation Form - for Checkboxes, Radio Buttons, and Text Fields [COLOR=000080]</TITLE>[/color]

<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
<!--
function validateCheckBoxes(checkboxList){

var checkboxValue = null;
for (var i=0; i < checkboxList.length; i++) {
if (checkboxList
Code:
[
i
Code:
]
.checked) {
checkboxValue = checkboxList
Code:
[
i
Code:
]
.value;
break;
}
}

if (checkboxValue == null) {
alert(&quot;Please be sure to check off a minimum of at least ONE checkbox.&quot;);
return false;
}else
return true; // in operation would be &quot;return true&quot;

}

function checkData(){

var vError = &quot;&quot;;
var vForm = document.Form;

if (vForm.first_name.value == &quot;&quot;){
vError += &quot;Please enter your first name in the field titled: 'First Name'\n&quot;;
}

if (vForm.last_name.value == &quot;&quot;){
vError += &quot;Please enter your last name in the field titled: 'Last Name'.\n&quot;
}

if (vForm.email.value == &quot;&quot;){
vError += &quot;Please enter your e-mail address in the field titled: 'E-mail Address'.&quot;;
}

if(vError == &quot;&quot;)
return true;
else{
alert(vError);
return false;
}

}

//-->
</script>

[COLOR=000080]<style type=&quot;text/css&quot;>[/color]
<!--

TD {
font-family: arial;
font-size: 10pt;
}

[COLOR=666666]//-->
[COLOR=000080]</style>[/color]
[/color]



[COLOR=000080]</HEAD>[/color]

[COLOR=000080]<BODY>[/color]

[COLOR=000080]<font face=&quot;arial&quot; size=&quot;2&quot;>[/color]
This form will [COLOR=000080]<b>[/color]first[COLOR=000080]</b>[/color] check the text fields ... and return a message for each one that wasn't completed.[COLOR=000080]<br>[/color]
[COLOR=000080]<br>[/color]
[COLOR=000080]<b>[/color]Next[COLOR=000080]</b>[/color], it will validate the 'checkboxes' and provide a separate reminder indicating that the user needs to check off at least ONE of the checkboxes in order
to submit the form.[COLOR=000080]<br>[/color]
[COLOR=000080]<br>[/color]

[COLOR=000080][COLOR=FF8000]<form name=&quot;Form&quot; method=&quot;post&quot;
action=&quot;/cgi-bin/FILENAME.cgi&quot;
onSubmit=&quot;return (validateCheckBoxes(this.size) && checkData())&quot;>[/color][/color]

[COLOR=008080]<table border=&quot;0&quot;>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td valign=&quot;top&quot;>[/color][COLOR=000080][COLOR=FF8000]<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;small&quot;>[/color][/color] Small[COLOR=000080]<br>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;medium&quot;>[/color][/color] Medium[COLOR=000080]<br>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;large&quot;>[/color][/color] Large[COLOR=000080]<br>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;checkbox&quot; name=&quot;size&quot; value=&quot;extra_large&quot;>[/color][/color] Extra Large[COLOR=000080]<br>[/color]
[COLOR=008080]</td>[/color]
[COLOR=008080]</tr>[/color]
[COLOR=008080]</table>[/color]

[COLOR=000080]<br>[/color]
First Name: [COLOR=000080][COLOR=FF8000]<input type=&quot;text&quot; name=&quot;first_name&quot; size=&quot;10&quot; maxlength=&quot;25&quot;>[/color][/color] Last Name: [COLOR=000080][COLOR=FF8000]<input type=&quot;text&quot; name=&quot;last_name&quot; size=&quot;15&quot; maxlength=&quot;35&quot;>[/color][/color][COLOR=000080]<br>[/color]
[COLOR=000080]<br>[/color]
Email Address: [COLOR=000080][COLOR=FF8000]<input type=&quot;text&quot; name=&quot;email&quot; size=&quot;25&quot; maxlength=&quot;50&quot;>[/color][/color][COLOR=000080]<br>[/color]
[COLOR=000080]<br>[/color]
[COLOR=000080]<div align=&quot;center&quot;>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;submit&quot; value=&quot;Submit&quot;>[/color][/color] [COLOR=000080][COLOR=FF8000]<input type=&quot;reset&quot; value=&quot;Reset&quot;>[/color][/color]
[COLOR=000080]</div>[/color]


[COLOR=000080][COLOR=FF8000]</form>[/color][/color]


[COLOR=000080]</font>[/color]

[COLOR=000080]</BODY>[/color]
[COLOR=000080]</HTML>[/color] - tleish
 
tleish,

Ok. That worked!! You're a genius!!!

But .. if you wouldn't mind ...

Could you explain exactly what you need to change or modify in comparison to what was there originally?

What magic did you do?

:))

Thanks again,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Here's the list of changes:

Overall:[ol][li]Combined to the Javascript Functions into one Javascript Tag[/li]
[/ol]function validateCheckBoxes(checkboxList)[ol][li]Changed validateCheckBoxes(checkboxName, checkboxList) to validateCheckBoxes(checkboxList) since checkboxName wasn't being used.[/li]
[li]checkboxList is an array, so checkboxList.length will return the number of checkboxes with the name specified. In order to check if a specific checkbox is checked, you need to access it via array (ex checkboxList
Code:
[1]
). So in the loop I changed if (checkboxList.checked) to if (checkboxList
Code:
[i]
.checked)
. The i is comming from the loop.[/li]
[li]Changed checkboxValue = checkboxList.value; to checkboxValue = checkboxList
Code:
[i]
.value;
for the same reason as the previous item. You really could just make this checkboxValue = 1[/li]
[li]Added return true to the function so that it will return either false or true[/li]
[/ol]function checkData()[ol][li]Replaced var correct = true; with var vError = &quot;&quot;; as a string that will store all the errors. At the end of the function, if vError is still empty, there's no errors.[/li]
[li]Added var vForm = document.Form; so that I could type vForm instead of document.Form in each of the if statements. It's just for convenience, and not necessary[/li]
[li]Changed all document.Form to vForm in the if() statements[/li]
[li]Removed correct = false; from all the if() statements[/li]
[li]Changed all alert(&quot;Text&quot;) to vError += &quot;Text\n&quot;. The += is a shortcut for writing vError = vError + &quot;Text\n&quot;. Then \n at then end of each string makes a carriage return so that the nect string (or error in this case) will be on new line in the final alert box.[/li]
[li]Changed return correct; to
if(vError == &quot;&quot;)
return true;
else{
alert(vError);
return false;

If the vError is &quot;&quot;, that means there were no errors (return true), else alert the list of error (alert(vError)) and return false.
[/li]
[/ol]Form:[ol][li]Removed onClick=&quot;return checkData()&quot; from the submit button[/li]
[li]Changed onSubmit=&quot;return validateCheckBoxes('size', this.size)&quot; to onSubmit=&quot;return (validateCheckBoxes(this.size) && checkData())&quot;. What this does is it first evaluates the 2 functions. if both functions resolve to true (true && true) then the will be onSubmit=&quot;return (true) and the form will submit. If 1 or both of the functions returns false (true && false) then the will be onSubmit=&quot;return (false) and the form will NOT submit&quot;[/li][/ol]Does that explain it? - tleish
 
THANK YOU ... Oh Tek-Tip &quot;Master of Javascript!&quot;

:)

Sincerely,
Gary

Thank you very, very much!
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top