Help with validation.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LugNut29
    New Member
    • Jul 2013
    • 16

    Help with validation.

    Hello,

    I have to validate that there is information and radio buttons have been selected. Also, I need to be able to reset the text boxes. When I click the submit button, it does not do the validation and when I click the reset button, it does nothing. Here's what I have, but it doesn't seem to work? Please help me with this.

    Thanks,
    Tom

    Code:
    <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <title>Scion tC Catalog</title>
    </head>
    <body>
    <script type="text/javascript">
    function validateForm()
    {
    var age=document.forms["myForm"]["fage"].value;
    var x=document.forms["myForm"]["fname"].value;
    var scion = getCheckedValue(document.forms["myForm"].elements["scion"]);
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
        if (isNaN(age)||age < 1||age > 100)
      {
     alert("Age must be between 1-100");
      return false;
     }
    function getCheckedValue(radioObj) {
        if(!radioObj)
            return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
            if(radioObj.checked)
                return radioObj.value;
            else
                return "";
        for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                return radioObj[i].value;
            }
        }
        return "";
    }
    if (cSelect.length == 0) {
        alert("Please give a rating");
    }
    else {alert(cSelect);
    }
    function formReset()
    {
    document.getElementById("frm1").reset();
    }
    </script>
    
    <a name="Top" id="Top"></a>
    <h1 class="">2010-14 Scion tC Catalog</h1>
    <table class="centered-table">
    <tr>
    <th><a href="index.php">Home</a></th>
    <th><a href="list.php">List</a></th>
    <th><a href="table.php">Table</a></th>
    </tr>
    </table>
    <br />
    <h2>2010-14 Scion tC Release Series and Base Models</h2>
    <p class="sansserif">The best of the Scion tC models<br />
    <br /></p>
    <form id="frm1" name="myForm" action="thankyou.php" onsubmit="return validateForm()" method="post">First name: <input type="text" name="fname" />  Age: <input type="text" name="fage" />  <input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" /></form>
    <br />
    <table class="table1">
    <tr>
    <th>Select</th>
    <th>Series</th>
    <th>Photo</th>
    <th>Description</th>
    <th>Price</th>
    </tr>
    <tr>
    <td><input type="radio" name="scion" id="r1" value="1" /><br /></td>
    <td>2010 Scion tC RS 6.0</td>
    <td><img src="pictures/2011S.jpg" alt="2010 Scion tC RS 6.0" /></td>
    <td>Speedway Blue: 1,100 units produced. If blue is your color, then this is the car for you!</td>
    <td>$22,999</td>
    </tr>
    <tr>
    <td><input type="radio" name="sicon" id="r2" value="2" /><br /></td>
    <td>2012 Scion tC RS 7.0</td>
    <td><img src="pictures/2012S.jpg" alt="2012 Scion tC RS 7.0" /></td>
    <td>High Voltage Yellow: 2,200 units produced. If yellow is your color, then this is the car for you!</td>
    <td>$23,999</td>
    </tr>
    <tr>
    <td><input type="radio" name="sicon" id="r3" value="3" /><br /></td>
    <td>2013 Scion tC RS 8.0</td>
    <td><img src="pictures/2013S.jpg" alt="2013 Scion tC RS 8.0" /></td>
    <td>Absolutely Red: 2,000 units produced. If red is your color, then this is the car for you!</td>
    <td>$24,999</td>
    </tr>
    <tr>
    <td><input type="radio" name="sicon" id="r4" value="4" /><br /></td>
    <td>2014 Scion tC Base Model</td>
    <td><img src="pictures/2014S.jpg" alt="2014 Scion tC Base Model" /></td>
    <td>If you love a nice sports car and easy on gas, then this is the car for you!</td>
    <td>$18,999</td>
    </tr>
    </table>
    </body>
    </html>
    Last edited by Rabbit; Aug 4 '13, 07:19 PM. Reason: Please use code tags when posting code or formatted data.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    have a look at the JavaScript Error Console.

    there are several issues:
    - the brace count does not match (hence your script shouldn’t run at all)
    - a form control value is never null
    - validateForm() never returns a truthy value

    Comment

    • LugNut29
      New Member
      • Jul 2013
      • 16

      #3
      Hi Dormilich,

      Thanks for the update. I did find the bracket issue, but I am not sure how to resolve the other issues? I'm not a programmer by any means, but I'm trying to learn. Where can I get help on the other two issues?

      T

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        issue #2: don’t test a form value for null

        issue #3: return true somewhere

        Comment

        • LugNut29
          New Member
          • Jul 2013
          • 16

          #5
          OK, so how do I do that? I don't understand what u mean by not testing a form value for null and returning true somewhere? Can you give me examples?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            not testing for null:
            Code:
            // instead of (cf. line #17)
            if (x==null || x=="")
            // do
            if (x.trim() === "") // exclude spaces, too
            // or
            if (!x)
            on line #67 you have the following:
            Code:
            onsubmit="return validateForm()"
            if validateForm() only returns false (lines #20, #25) or undefined (line #47 or whereever the function ends), it will evaluate as
            Code:
            onsubmit="return false"
            so you can never submit the form until you disable JavaScript.

            Comment

            • LugNut29
              New Member
              • Jul 2013
              • 16

              #7
              Thanks for those examples. The I think I have everything working except one thing. When I submit the form with out selecting a radio button, it checks it and says that I need to select a radio button, but it also submits and still takes it to the complete page. I'm not sure where in the function getCheckedValue () or function validateForm() it letting the form move past to the thank you page? I need it to stop everything if the name, age, and a radio button is not selected. Do you see anything in my code that would make this happen?

              Code:
              catalog.js
              
              function formReset() {
              document.getElementById("frm1").reset(); }
              
              function getCheckedValue(radioObj) {
                  if(!radioObj)
                      return "";
                  var radioLength = radioObj.length;
                  if(radioLength == undefined)
                      if(radioObj.checked)
                          return radioObj.value;
                      else
                          return "";
                  for(var i = 0; i < radioLength; i++) {
                      if(radioObj[i].checked) {
                          return radioObj[i].value;
                      }
                  }
                  return "";
              }
              
              function validateForm()
              {
              var x=document.forms['myForm']['fname'].value;
              if (x.trim() === "")
                {
                alert("First name must be filled out");
                return false;
                }
              var age=document.forms['myForm']['age'].value;
              if (isNaN(age)||age<1||age>100) {
               alert("Age must be between 1-100");
                return false;
                }
              var cSelect = getCheckedValue(document.forms['myForm'].elements['scion']);
              if (cSelect.length == 0) {
                  alert("Please select a tC Model");
              }
              else {alert(cSelect);}
              }
              Page w/Form and Radio Button

              Code:
              <form id="frm1" name="myForm" onsubmit="return (validateForm() && checkRadios())" action="thankyou.php" method="post">First name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input type="submit" value="Submit" /> <input type="button" onclick="formReset()" value="Reset form" />
              <br />
              <table class="table1">
              <tr>
              <th>Select</th>
              <th>Series</th>
              <th>Photo</th>
              <th>Description</th>
              <th>Price</th>
              </tr>
              <tr>
              <td class="radio button"><input type="radio" name="scion" id="r1" method="post" value="You selected the 2010 Scion tC RS" /><br /></td>
              <td class="2010 scion">2010 Scion tC RS 6.0</td>
              <td class="2010 image"><img src="pictures/2011S.jpg" alt="2010 Scion tC RS 6.0" </td>
              <td class="2010 description">Speedway Blue: 1,100 units produced. If blue is your color, then this is the car for you!</td>
              <td class="2010 price">$22,999</td>
              </tr>
              <tr>
              Last edited by Dormilich; Aug 8 '13, 06:37 AM.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                hm, this shouldn’t submit at all.

                Comment

                Working...