Welcome to Dream.In.Code
Become an Expert!

Join 149,534 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,534 people online right now. Registration is fast and FREE... Join Now!




Calling Multiple Functions onSubmit

 
Reply to this topicStart new topic

Calling Multiple Functions onSubmit, how to apply multi onsubmit return function?

jilong
5 Jun, 2007 - 03:12 AM
Post #1

New D.I.C Head
*

Joined: 17 May, 2007
Posts: 32


My Contributions
In my form i want to check for 3 things.
1. word count limit
2. required fields
3. agree term

below are the 3 function script i did , it work if i call 1 of the function, but if i call 3 function together it not work.

please help me on this, thanks

<form name="agreeform" onSubmit="return defaultagree(this); checkwords(this); checkrequired(this)">

CODE

<script>
<!--
function checkwords(cur){
var maxwords=10
if (document.images){
var temp=cur.words.value.split(" ")
if (temp.length>maxwords){
alert("Please restrain your input to 10 or less words!")
return false
}
}
}
//-->
</script>

<script>
function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
pass=false
break
}
}
}
}
if (!pass){
alert("One or more of the required elements are not completed. Please complete them, then submit again!")
return false
}
else
return true
}
</script>

<script>
var checkobj

function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){  //hunt down submit button
var tempobj=checkobj.form.elements[i]
if(tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}

function defaultagree(el){
if (!document.all&&!document.getElementById){
if (window.checkobj&&checkobj.checked)
return true
else{
alert("Please read/accept terms to submit form")
return false
}
}
}

</script>

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calling Multiple Functions OnSubmit
5 Jun, 2007 - 05:38 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
The better way to implement would be to build a function that acts as an overall guide...something like performActions(this);

In that function, call the three functions you need.
User is offlineProfile CardPM
+Quote Post

supersloth
RE: Calling Multiple Functions OnSubmit
5 Jun, 2007 - 09:09 AM
Post #3

serial frotteur - RUDEST MEMBER ON D.I.C.
Group Icon

Joined: 21 Mar, 2001
Posts: 19,904



Thanked: 13 times
Dream Kudos: 2147483647
Expert In: being gentlemanly

My Contributions
agreed. also, i think the direct problem might have something to do with using the return on only one of the functions, i believe it's expecting you to do something with the value, but the way it's built that appears unnecessary.
User is offlineProfile CardPM
+Quote Post

Oaty
RE: Calling Multiple Functions OnSubmit
5 Jun, 2007 - 09:15 AM
Post #4

New D.I.C Head
*

Joined: 10 May, 2007
Posts: 28


My Contributions
I agree with Amadeus, with one extra bit, within your performActions function,
if any of the sub functions return false you want performActions to return false.

The reason 1 function works and not the other is that as soon as the first function to return true returns your form is being submitted and your subsequent functions are not being called.

User is offlineProfile CardPM
+Quote Post

jilong
RE: Calling Multiple Functions OnSubmit
5 Jun, 2007 - 11:10 PM
Post #5

New D.I.C Head
*

Joined: 17 May, 2007
Posts: 32


My Contributions
em..i actually not really know how to use the performActions(this);, But you guy give me a ideal to put three function together, finally i come out this, and it work.

Thank alot! biggrin.gif

CODE

<form action="form.php" method="post"   enctype="multipart/form-data" name="myForm" onSubmit="return checkFields(this)">
-
-
-
I accept: <input type="checkbox" value="0" name="agree">
    <input type="submit" name="Submit" value="Submit">    



CODE
<script LANGUAGE="JavaScript">


<!-- Begin
function checkFields(f) {
name = document.myForm.name.value;
email = document.myForm.email.value;
company = document.myForm.company.value;
telephone = document.myForm.telephone.value;

//check required field
if ((name == "") || (email == "")|| (company == "")|| (telephone == "") ) {
alert("Please enter your Name, Email address, Company and Telephone Number.");
return false;
}

// check agree term before submit
if (f.agree.checked == false )
{
alert('Please check the box to continue.');
return false;
}else
return true;
}

//  End -->
</script>



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month