www.webdeveloper.com
Results 1 to 3 of 3

Thread: Multiple onSubmits with returns

  1. #1
    Join Date
    Jun 2005
    Posts
    201

    Multiple onSubmits with returns

    Hello. I have a form that has three onSubmit functions to the form. The problem is that two of the functions have a return value while the other doesn't. I can't get the form to run all three scripts correctly onSubmit. Here is an example of the form tag:

    HTML Code:
    <form action="registration.php" method="post" name="reg_form" id="packages" onsubmit="return Form1_Validator(this) && checkTheBox(); verify(this.email, this.emailtwo);">
    The verify() function does not return a true or false. If that script could be modified to return a true or false it may be problem solved. Here is the verify function:
    Code:
    <script type = "text/javascript">
    function checkTheBox() {
    var flag = 0;
    for (var i = 0; i< 5; i++) {
    if(document.reg_form["Package[]"][i].checked){
    flag ++;
    }
    }
    
    for (var i = 0; i< 10; i++) {
    if(document.reg_form["ALaCarte[]"][i].checked){
    flag ++;
    }
    }
    
    if (flag == 0) {
    alert ("You must check at least one checkbox!!");
    return false;
    }
    return true;
    }
    </script>
    Thanks
    Barkleigh's Webmaster

  2. #2
    Join Date
    Apr 2009
    Posts
    48
    can you please elaborate what is the use of this code: Form1_Validator(this) && checkTheBox();

  3. #3
    Join Date
    Jun 2005
    Posts
    201
    Sure. The checkTheBox() script makes sure at least 1 checkbox is checked on the form before submission. The Form1_Validator(this) makes sure there is input in certain text fields.


    Code:
    <script language="JavaScript">
    <!--
    function Form1_Validator(theForm)
    {
    if (theForm.name.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.name.focus();
    theForm.name.style.background='#FFFF99';
    return (false);
    }
    if (theForm.address.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.address.focus();
    theForm.address.style.background='#FFFF99';
    return (false);
    }
    if (theForm.city.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.city.focus();
    theForm.city.style.background='#FFFF99';
    return (false);
    }
    if (theForm.state.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.state.focus();
    theForm.state.style.background='#FFFF99';
    return (false);
    }
    if (theForm.zip.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.zip.focus();
    theForm.zip.style.background='#FFFF99';
    return (false);
    }
    if (theForm.phone.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.phone.focus();
    theForm.phone.style.background='#FFFF99';
    return (false);
    }
    if (theForm.email.value == "")
    	{
    alert("Please enter a value for this field.")
    theForm.email.focus();
    theForm.email.style.background='#FFFF99';
    return (false);
    }
    
    
    return (true);
    } 
    
    //-->
    
    </script>
    Last edited by bp_travis; 05-05-2009 at 09:04 AM.
    Barkleigh's Webmaster

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles