Solved

Form validation not firing onSubmit()

Posted on 2006-11-18
2
2,302 Views
Last Modified: 2009-12-16
Can someone clue me into why my validation function is not firing when the form is submitted?  Here is the page:


<html>
  <head>
    <title>Team One Networking Intranet</title>
    <link href="/style.css" rel="stylesheet" type="text/css" media="screen"/>
      <script Language="JavaScript" Type="text/javascript"><!--
            function FormValidator(myForm)
            {

            if (myForm.ManufacturerPartNumber.value == "")
            {
                  alert("Please enter a value for the \"Manufacturer Part Number\" field.");
                  myForm.ManufacturerPartNumber.focus();
                  return (false);
            }

            if (myForm.ManufacturerPartNumber.value.length > 32)
            {
                  alert("Please enter at most 32 characters in the \"Manufacturer Part Number\" field.");
                  myForm.ManufacturerPartNumber.focus();
                  return (false);
            }

            if (myForm.UnitPrice.value == "")
            {
                  alert("Please enter a value for the \"UnitPrice\" field.");
                  myForm.UnitPrice.focus();
                  return (false);
            }

            var checkOK = "0123456789-.,";
            var checkStr = myForm.UnitPrice.value;
            var allValid = true;
            var validGroups = true;
            var decPoints = 0;
            var allNum = "";
            for (i = 0;  i < checkStr.length;  i++)
            {
                  ch = checkStr.charAt(i);
                  for (j = 0;  j < checkOK.length;  j++)
                  if (ch == checkOK.charAt(j))
                        break;
                  if (j == checkOK.length)
                  {
                  allValid = false;
                  break;
                  }
                  if (ch == ".")
                  {
                  allNum += ".";
                  decPoints++;
                  }
                  else if (ch == "," && decPoints != 0)
                  {
                  validGroups = false;
                  break;
                  }
                  else if (ch != ",")
                  allNum += ch;
            }
            if (!allValid)
            {
                  alert("Please enter only digit characters in the \"UnitPrice\" field.");
                  myForm.UnitPrice.focus();
                  return (false);
            }

            if (decPoints > 1 || !validGroups)
            {
                  alert("Please enter a valid number in the \"UnitPrice\" field.");
                  myForm.UnitPrice.focus();
                  return (false);
            }

            if (myForm.Quantity.value == "")
            {
                  alert("Please enter a value for the \"Quantity\" field.");
                  myForm.Quantity.focus();
                  return (false);
            }

            var checkOK = "0123456789-.,";
            var checkStr = myForm.Quantity.value;
            var allValid = true;
            var validGroups = true;
            var decPoints = 0;
            var allNum = "";
            for (i = 0;  i < checkStr.length;  i++)
            {
                  ch = checkStr.charAt(i);
                  for (j = 0;  j < checkOK.length;  j++)
                  if (ch == checkOK.charAt(j))
                        break;
                  if (j == checkOK.length)
                  {
                  allValid = false;
                  break;
                  }
                  if (ch == ".")
                  {
                  allNum += ".";
                  decPoints++;
                  }
                  else if (ch == "," && decPoints != 0)
                  {
                  validGroups = false;
                  break;
                  }
                  else if (ch != ",")
                  allNum += ch;
            }
            if (!allValid)
            {
                  alert("Please enter only digit characters in the \"Quantity\" field.");
                  myForm.Quantity.focus();
                  return (false);
            }

            if (decPoints > 1 || !validGroups)
            {
                  alert("Please enter a valid number in the \"Quantity\" field.");
                  myForm.Quantity.focus();
                  return (false);
            }

            if (myForm.ShortDescription.value == "")
            {
                  alert("Please enter a value for the \"Short Description\" field.");
                  myForm.ShortDescription.focus();
                  return (false);
            }

            if (myForm.ShortDescription.value.length > 256)
            {
                  alert("Please enter at most 256 characters in the \"Short Description\" field.");
                  myForm.ShortDescription.focus();
                  return (false);
            }

            //--></script>
  </head>
  <body>

 
  <h1>Add Product</h1>

    <form action="action.asp" method="post" name="myForm" onsubmit="return FormValidator(this);">
            <input type="hidden" name="ID" value="637" ID="Hidden1">
            <input type="hidden" name="hAction" value="AddRow" ID="Hidden2">
            
            <table width="100%" class="noborder">
                  <tr>
                        <td>Part Number:<br><input type="text" name="ManufacturerPartNumber" value="" ID="Text1"></td>

                        <td>Taxable:<br><input type="checkbox" name="Taxable" ID="Checkbox1"></td>
                  </tr>
                  <tr>
                        <td>Quantity:<br><input type="text" name="Quantity" ID="Text3"></td>
                        <td>Unit Price:<br><input type="text" name="UnitPrice" value="" ID="Text2"></td>
                  </tr>
                  <tr>

                        <td colspan="2">Short Description:<br><input type="text" name="ShortDescription" value="" ID="Text5" size="55"></td>
                  </tr>
            </table>

            <p class="actionbuttons"><input type="submit" name="cmdAction" value="Add" ID="Submit1">&nbsp;<input type="submit" name="cmdAction" value="Close" onclick="self.close();" ID="Submit2"></p>

    </form>

  </body>
</html>

0
Comment
Question by:DanLockwood
2 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 17973138
Add one curly brace for the function end and you are done.
Like this:
          }
          //--></script>
  </head>

0
 

Author Comment

by:DanLockwood
ID: 17974877
I knew it was something simple.  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now