Solved

Form validation not firing onSubmit()

Posted on 2006-11-18
2
2,297 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

760 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