Solved

Form validation not firing onSubmit()

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

776 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