Solved

Form validation not firing onSubmit()

Posted on 2006-11-18
2
2,335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

688 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