Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Validating User Input - NOT WORKING

    The assignment is to take a form and determine whether or not the user has put in data into the parts of the form. We are using Javascript to validate the form input and html to create the form itself. My code was running before, but then I added a confirm user input at the bottom of the form and it stopped working. I have no idea what to do at this point. Any help would be useful. I don't have any error messages because I don't know how to test it in the firefox javascript console, if someone wants to instruct me on this i can get back to you guys with the error messages. Thanks Also, my submit button has not been running the validate program when selected.

    Code:
    <html>
    <head>
    <title>Order Form</title>
    <script language="javascript">
    
    function validate(objForm)
    {
    str="";
    checkstr="";
    checkstr2="";
    stateselect = "false";
    
    //VALIDATING CONTACT INFORMATION
      if (objForm.firstName.value == "")
       {
    	str+= "You must enter your First Name\n";
       }
      if (objForm.lastName.value == "")
       {
    	str+= "You must enter your Last Name\n";
       }
      if (objForm.phoneNumber.value == "")
       {
    	str+= "You must enter a Phone Number\n";
       }
    
    //VALIDATING SHIPPING ADDRESS
      if (objForm.address.value == "")
       {
    	str+= "You must enter an address\n";
       }
      if (objForm.city.value == "")
       {
    	str+= "You must enter a city\n";
       }
      for(i=1; i<objForm.state.options.length; i++)
      {
         if (objForm.state.options[i].selected) stateselect = "true";
      }
      if (stateselect != "true") str+= "You must select a state\n";
      if(isNaN(myForm.zipcode.value)||myForm.zipcode.value.length!=5)
      {
    	str+= "You must enter a 5 digit zipcode\n";
      }
    //VALIDATING CHECK BOX
      if (!objForm.doublechocchip.checked && !objForm.vanillachip.checked && !objForm.zebra.checked && !objForm.tiger.checked && !objForm.choccoveredstrawberry.checked && !objForm.mintcchochip.checked
     && !objForm.raspberry.checked && !objForm.redvelvet.checked && !objForm.kailua.checked && !objForm.blackforest.checked && !objForm.peanutbutter.checked && !objForm.mandm.checked
     && !objForm.oreo.checked && !objForm.mocha.checked && !objForm.gingerbread.checked && !objForm.pumpkin.checked && !objForm.pistachio.checked && !objForm.lavender.checked
     && !objForm.carrotcake.checked && !objForm.strawberrybanana.checked) 
      str+= "You must select at least one cupcake\n";
    
    //VALIDATING CHECKED BOXES
      if (objForm.doublechocchip.checked)  checkstr += objForm.doublechocchip.value + ", ";
      if (objForm.vanillachip.checked)  checkstr += objForm.vanillachip.value + ", ";
      if (objForm.zebra.checked)  checkstr += objForm.zebra.value + ", ";
      if (objForm.tiger.checked)  checkstr += objForm.tiger.value + ", ";
      if (objForm.choccoveredstrawberry.checked)  checkstr += objForm.choccoveredstrawberry.value + ", ";
      if (objForm.mintchocchip.checked)  checkstr += objForm.mintchocchip.value + ", ";
      if (objForm.raspberry.checked)  checkstr += objForm.raspberry.value + ", ";
      if (objForm.redvelvet.checked)  checkstr += objForm.redvelvet.value + ", ";
      if (objForm.kailua.checked)  checkstr += objForm.kailua.value + ", ";
      if (objForm.blackforest.checked)  checkstr += objForm.blackforest.value + ", ";
      if (objForm.peanutbutter.checked)  checkstr += objForm.peanutbutter.value + ", ";
      if (objForm.mandm.checked)  checkstr += objForm.mandm.value + ", ";
      if (objForm.oreo.checked)  checkstr += objForm.oreo.value + ", ";
      if (objForm.mocha.checked)  checkstr += objForm.mocha.value + ", ";
      if (objForm.gingerbread.checked)  checkstr += objForm.gingerbread.value + ", ";
      if (objForm.pumpkin.checked)  checkstr += objForm.pumpkin.value + ", ";
      if (objForm.pistachio.checked)  checkstr += objForm.pistachio.value + ", ";
      if (objForm.lavender.checked)  checkstr += objForm.lavender.value + ", ";
      if (objForm.carrotcake.checked)  checkstr += objForm.carrotcake.value + ", ";
      if (objForm.strawberrybanana.checked)  checkstr += objForm.starwberrybanana.value + ", ";
    
    //VALIDATING CHECK BOX
      if (!objForm.mini.checked && !objForm.regular.checked && !objForm.collossal.checked) 
      str+= "You must select a size\n";
    
    //VALIDATING CHECKED BOXES
      if (objForm.mini.checked)  checkstr2 += objForm.mini.value + ", ";
      if (objForm.regular.checked)  checkstr2 += objForm.regular.value + ", ";
      if (objForm.collossal.checked)  checkstr2 += objForm.collossal.value + ", ";
    
    //print all form input
    if (str != ""){
     alert(str);
     return false;
    }
    else{
     str= "Please Confirm Your Information:";
     str += "\nFirst Name: " +objForm.firstName.value;
     str += "\nLast Name: " +objForm.lastName.value;
     str += "\nPhone Number: " +objForm.phoneNumber.value;
     str += "\nAddress: " +objForm.address.value;
     str += "\nCity: " +objForm.city.value;
     str += "\nZip Code: " +objForm.zipcode.value;
     str += "\nCupcakes: " +checkstr;
     str += "\nSize: " +checkstr2;
     alert(str);
     return true;
    }
    }
    </script>
    </head>
    <link rel="stylesheet" type="text/CSS" href="CellFormating.css"/>
    <body onLoad="document.forms[0].elements[0].focus()">
    <h2 align="center" class="CellFormat1"> Please Use the Following Form to Place Your Order: </h2>
    <form id ="CellFormat2" name="myForm" method="post" action="orderconfirmation.html" onSubmit="return validate(myForm)">
    <table align="center">
    <tr>
    <td id="CellFormat4">Contact Information:</td>
    
    </tr>
    <tr>
    <td>First Name: </td>
    <td><input name="firstName" size="15" /></td>
    </tr>
    <tr>
    <td>Last Name: </td>
    <td><input type="text" name="lastName" size="15" maxlength="20" /></td>
    </tr>
    <tr>
    <td>Phone Number: </td>
    <td><input type="text" name="phoneNumber" size="10" maxlength="10" /></td>
    </tr>
    </table>
    
    <p>
    </p>
    <table align="center">
    <tr>
    <td id="CellFormat4">Shipping Address:</td>
    </tr>
    <td>Address: </td>
    <td><input type="text" name="address" size="22" maxlength="30" /></td>
    </tr>
    <tr>
    <td>City: </td>
    <td><input type="text" name="city" size="10" maxlength="15" /></td>
    </tr>
    <tr>
    
    <td>State: </td>
    <td>
    <select name="state" size="1">
    <option selected>Select your State</option>
    <option>Alabama</option>
    <option>Alaska</option>
    <option>Arizona</option>
    <option>Arkansas</option>
    <option>California</option>
    <option>Colorado</option>
    
    <option>Connecticut</option>
    <option>Delaware</option>
    <option>Florida</option>
    <option>Georgia</option>
    <option>Hawaii</option>
    <option>Idaho</option>
    <option>Illinois</option>
    <option>Indiana</option>
    <option>Iowa</option>
    
    <option>Kansas</option>
    <option>Kentucky</option>
    <option>Louisiana</option>
    <option>Maine</option>
    <option>Maryland</option>
    <option>Massachusetts</option>
    <option>Michigan</option>
    <option>Minnesota</option>
    <option>Mississippi</option>
    
    <option>Missouri</option>
    <option>Montana</option>
    <option>Nebraska</option>
    <option>Nevada</option>
    <option>New Hampshire</option>
    <option>New Jersey</option>
    <option>New Mexico</option>
    <option>New York</option>
    <option>North Carolina</option>
    
    <option>North Dakota</option>
    <option>Ohio</option>
    <option>Oklahoma</option>
    <option>Oregon</option>
    <option>Pennsylvania</option>
    <option>Rhode Island</option>
    <option>South Carolina</option>
    <option>South Dakota</option>
    <option>Tennessee</option>
    
    <option>Texas</option>
    <option>Utah</option>
    <option>Vermont</option>
    <option>Virginia</option>
    <option>Washington</option>
    <option>West Virginia</option>
    <option>Wisconsin</option>
    <option>Wyoming</option>
    </select>
    
    </td>
    </tr>
    <tr>
    <td>Zip Code: </td>
    <td><input type="text" name="zipcode" size="10" maxlength="15" /></td>
    </tr>
    </table>
    <p>
    </p>
    
    <table align="center">
    <tr>
    <td><div align="center" id="CellFormat4">Select Your Cupcakes:</div></td>
    <td><div align="center" id="CellFormat4">Select Your Size(s):</div></td>
    
    <td><div align="center" id="CellFormat4">Select The Amount:</div></td>
    </tr>
    <tr>
    <td><input type="checkbox"name="doublechocchip" value="doublechocchip"/>Double Chocolate Chip</td>
    <td><input type="checkbox"name="mini" value="mini"/>Mini</td>
    <td>Amount: <input type="text" name="amt" size="5" maxlength="10" /></td>
    </tr>
    <tr>
    <td><input type="checkbox"name="vanillachip" value="vanillachip"/>Vanilla Chip</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="zebra" value="zebra"/>Zebra</td>
    
    <td><input type="checkbox"name="regular" value="regular"/>Regular</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="tiger" value="tiger"/>Tiger</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="choccoveredstrawberry" value="choccoveredstrawberry"/>Chocolate Covered Strawberry</td>
    <td><input type="checkbox"name="collosal" value="collossal"/>Collossal</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="mintchocchip" value="mintchocchip"/>Mind Chocolate Chip</td>
    </tr>
    
    <tr>
    <td><input type="checkbox"name="raspberry" value="raspberry"/>Raspberry</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="redvelvet" value="redvelvet"/>Red Velvet</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="kailua" value="kailua"/>Kailua</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="blackforest" value="blackforest"/>Black Forest</td>
    </tr>
    <tr>
    
    <td><input type="checkbox"name="peanutbutter" value="peanutbutter"/>Peanut Butter</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="mandm" value="mandm"/>M&M's</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="oreo" value="oreo"/>Oreo</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="mocha" value="mocha"/>Mocha</td>
    </tr>
    <tr>
    
    <td><input type="checkbox"name="gingerbread" value="gingerbread"/>Gingerbread</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="pumpkin" value="pumpkin"/>Pumpkin</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="pistachio" value="pistachio"/>Pistachio</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="lavender" value="lavender"/>Lavender</td>
    </tr>
    <tr>
    <td><input type="checkbox"name="carrotcake" value="carrotcake"/>Carrot Cake</td>
    
    </tr>
    <tr>
    <td><input type="checkbox"name="strawberrybanana" value="strawberrybanana"/>Strawberry Banana</td>
    </tr>
    </table>
    <p>
    </p>
    
    <div align="center" id="CellFormat4">Payment Method:</div>
    
    <table align="center">
    <tr>
    <td>Credit Card Type: </td>
    </tr>
    
    <tr>
    <td>Visa:<input type="radio" name="visa" value="visa" /></td>
    <td>Master Card:<input type="radio" name="master" value="master" /></td>
    <td>Discovery:<input type="radio" name="discovery" value="discovery" /></td>
    <td>Chase:<input type="radio" name="chase" value="chase" /></td>
    </tr>
    <tr>
    <td>Credit Card Number: </td>
    <td><input type="text" name="creditNumber" size="12" maxlength="12" /></td>
    </tr>
    <tr>
    <td>Expiration Date: </td>
    
    <td><input type="text" name="ExpDate" size="5" maxlength="5" /></td>
    </tr>
    <tr>
    <td>Security Code: </td>
    <td><input type="text" name="SecurityCode" size="3" maxlength="3" /></td>
    </tr>
    </table>
    <p>
    </p>
    
    <table align="center">
    <tr>
    <td><textarea rows="10" cols="25" wrap="soft">Please let us know how we are doing</textarea></td>
    </tr>
    </table>
    
    <table align="center">
    <tr>
    <td><input type="submit" name="Order" value="Submit Order" onSubmt="return validate(myForm)";/></td>
    <td><input type="button" name="confirm" value="Confirm Information" onClick="validate(myForm)"/></td>
    <td><input type="reset"  name="Clear" value="Reset"/></td>
    </tr>
    </table>
    </form>
    
    <table align="center">
    <tr>
    <td><a href="cupcakes.htm">View the Cupcakes</a></td>
    <td><a href="home.html">Go Home!</a></td>
    </tr>
    
    </table>
    </body>
    </html>

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by sruppert View Post
    The assignment is to take a form and determine whether or not the user has put in data into the parts of the form.
    I think you have weakness in your validation.

    With

    Code:
    if (objForm.firstName.value == "")
    if the user enters say a blank character or !@#$% or anything similar, the test condition in the IF will evaluate to false and so accept those characters as the first name.

    The validation should really check to see if the input contains only valid characters for that input. Using a regular expression is one option.
    Last edited by bullant; 04-18-2011 at 02:59 AM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You really ought to learn how to use your error console or better still Firebug. It is asking a lot to expect others to do this simple validation for you.

    This would reveal syntax/spelling errors such as
    !objForm.mintcchochip.checked
    and
    <input type="checkbox"name="collosal" value="collossal"/>Collossal</td>

    As bullant has pointed out, form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    Be aware that <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 04-18-2011 at 09:10 AM.

  • Users who have thanked Philip M for this post:

    sruppert (04-21-2011)


  •  

    Posting Permissions

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