Solved

Form validation not firing onSubmit()

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

732 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