www.webdeveloper.com
Results 1 to 2 of 2

Thread: Validating using Nested If statements

  1. #1
    Join Date
    May 2010
    Posts
    9

    Validating using Nested If statements

    ARGHH everything I try seems to not work. Could you give me a hint or a hand please?
    I am trying to validate the following form.. by using nested if statements. I want to simply add an onclick event to the submit button and then add a function to the web page. Then I tried to add the code to validate one field and get it working. I figured I could easily uplicate the code for one field via copy and paste and validate the next.
    Thanks for any help.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Other ways to help</title>



    <SCRIPT TYPE="text/javascript">
    <!--
    function dropdown(mySel)
    {
    var myWin, myVal;
    myVal = mySel.options[mySel.selectedIndex].value;
    if(myVal)
    {
    if(mySel.form.target)myWin = parent[mySel.form.target];
    else myWin = window;
    if (! myWin) return true;
    myWin.location = myVal;
    }
    return false;
    }
    //-->
    </SCRIPT>


    </head>


    <body>
    <a name="other">
    <body background="otherbackground.jpg">

    <center><table border="2" cellspacing="2" width="900" height="190" bordercolor="#000000">
    <tr>
    <td width="60%" td bgcolor="white">
    <b>To make a donation, fill and submit the following form. You can also make a cash donation directly by filling out your credit card information.<br/>
    Also, be sure to check yes if you would like to subscribe to our online monthly newsletter.</b><br/>
    <form name="Donations" method="post" action="mailto:chmcmil1@hacc.edu">
    First Name<input type="text" name="First Name" size="25" tabindex="1">
    M.I.<input type="text" name="M.I." size="1">
    Last Name<input type="text" name="Last Name" size="25"><br/>
    Street Address<input type="text" name="Street Address" size="25">
    Street Address 2<input type="text" name="Street Address 2" size="25"><br/>
    City<input type="text" name="City" size="20">
    State<input type="text" name="State" size="2">
    Zip<input type="text" name="Zip Code" size="10"maxlength="9"><br/>
    Email<input type="text" name="Email" size="25"><br/>
    Credit Card Number<input type="text" name="Credit Card Number" size="10"maxlength="8">
    Expiration Date<input type="text" name="Expiration Date" size="5">
    Security Number<input type="text" name="Security Number" size="3"maxlength="3">
    <textarea cols="50" rows="4" name="Comments">Comments or Questions:</textarea><br/>
    Subscribe to newsletter? <input type="radio" name="choices" value="Yes">Yes<input type="radio" name="choices" value="No">No<br/><br/>
    <input type="submit" value="Submit">
    <input type="reset">
    </form><br/>

    </table><br/>


    <FORM
    ACTION="../cgi-bin/redirect.pl"
    METHOD=POST onSubmit="return dropdown(this.gourl)">
    <SELECT NAME="gourl">
    <OPTION VALUE="">Choose a Destination...

    <OPTION VALUE="home.html">Home Page
    <OPTION VALUE="dogs.html">Learn About Dogs
    <OPTION VALUE="cats.html">Learn About Cats
    <OPTION VALUE="exotics.html">Learn About Exotics
    <OPTION VALUE="other.html">Other Ways To Help

    </SELECT>

    <INPUT TYPE=SUBMIT VALUE="Go">
    </FORM>
    <br/><br?>



    </body>


    </html>

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    You have 2 forms in your web page. I assume it's the 1st form you want to validate.

    Maybe use the validateForm(this) in the code below as a template.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Other ways to help</title>
     
    <
    SCRIPT TYPE="text/javascript">
    <!--
    function 
    dropdown(mySel)
    {
    var 
    myWinmyVal;
    myVal mySel.options[mySel.selectedIndex].value;
    if(
    myVal)
    {
    if(
    mySel.form.target)myWin parent[mySel.form.target];
    else 
    myWin window;
    if (! 
    myWin) return true;
    myWin.location myVal;
    }
    return 
    false;
    }
    //--------------------------------------------------------
     
    function validateForm(form) {
       
    //store all the form data in variables - only 1 input field shown for demo
       
    var fName form.txtFname.value;
     
       
    //initialise 'is data valid' flag.
       
    var dataIsValid true;
       
       
    //initialise error message string
       
    var errMsg "The following inputs are missing or invalid:\n\n";
       
       
    //validate first name
       
    if(fName == '') { //add your own additional validation rules for fName
           
    errMsg errMsg "First Name\n";
           
    dataIsValid false;
       }
       
       
    //validate last name
       
    if(lName == '') { //add your own additional validation rules for lName
           
    errMsg errMsg "Last Name\n";
           
    dataIsValid false;
       }
       
       
    //continue validating all the other input fields in the form
       
       //then display any error messages or submit the form if all input data is valid
       
    if(dataIsValid) { //all form data is valid, so submit the form
           
    return true;
       } else { 
    //some or all form data is invalid, so don't submit the form
           
    alert(errMsg);
           return 
    false;
       }   
    }
     
    //-->
    </SCRIPT>

    </head>

    <body>
    <a name="other">
    <body background="otherbackground.jpg">
    <center><table border="2" cellspacing="2" width="900" height="190" bordercolor="#000000">
    <tr>
    <td width="60%" td bgcolor="white">
    <b>To make a donation, fill and submit the following form. You can also make a cash donation directly by filling out your credit card information.<br/> 
    Also, be sure to check yes if you would like to subscribe to our online monthly newsletter.</b><br/>
    <form name="Donations" method="post" action="#" onsubmit="validateForm(this);">
    First Name<input type="text" name="txtFname" size="25" tabindex="1">
    M.I.<input type="text" name="M.I." size="1">
    Last Name<input type="text" name="Last Name" size="25"><br/>
    Street Address<input type="text" name="Street Address" size="25">
    Street Address 2<input type="text" name="Street Address 2" size="25"><br/>
    City<input type="text" name="City" id="myCity" size="20">
    State<input type="text" name="State" size="2">
    Zip<input type="text" name="Zip Code" size="10"maxlength="9"><br/>
    Email<input type="text" name="Email" size="25"><br/>
    Credit Card Number<input type="text" name="Credit Card Number" size="10"maxlength="8">
    Expiration Date<input type="text" name="Expiration Date" size="5">
    Security Number<input type="text" name="Security Number" size="3"maxlength="3">
    <textarea cols="50" rows="4" name="Comments">Comments or Questions:</textarea><br/>
    Subscribe to newsletter? <input type="radio" name="choices" value="Yes">Yes<input type="radio" name="choices" value="No">No<br/><br/>
    <input type="submit" value="Submit">
    <input type="reset">
    </form><br/>
    </table><br/>

    <FORM 
    ACTION="../cgi-bin/redirect.pl" 
    METHOD=POST onSubmit="return dropdown(this.gourl)">
    <SELECT NAME="gourl">
    <OPTION VALUE="">Choose a Destination...
    <OPTION VALUE="home.html">Home Page
    <OPTION VALUE="dogs.html">Learn About Dogs
    <OPTION VALUE="cats.html">Learn About Cats
    <OPTION VALUE="exotics.html">Learn About Exotics
    <OPTION VALUE="other.html">Other Ways To Help
    </SELECT>
    <INPUT TYPE=SUBMIT VALUE="Go">
    </FORM>
    <br/><br?>
     
    </body>

    </html> 
    Last edited by tirna; 05-14-2010 at 12:41 AM.

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