[Javascript] validating a form using Javascript

Cheryl Kirkpatrick cheryl at leo.scsl.state.sc.us
Wed Jan 22 10:48:20 CST 2003


I need your help again! Someone always has a solution to my problem so I

have my fingers crossed!

I am using ColdFusion to input data into an Access database.
My instructions are to include:

A default value of "-2" in all fields and then performs an edit check
for that "-2" to force the user to replace the "-2".

Validate all addition used on the form.

I wrote a JavaScript function that checks for "-2" when the form is
submitted and gives the user a message to change the incorrect field.
It then checks the addition on the form.  I thought it was working fine
but found problems during testing.

If there is a space before the "-2" the JavaScript doesn't catch it.
Also when I used amounts over 999, ColdFusion added a comma and the
addition part of the JavaScript did not function. I fixed this by using
the NumberFormat tag of ColdFusion but while the part of the JavaScript
that validates additions works, the
part that checks for "-2" does not work at all! If I remove the mask for

the NumberFormat, the check for "-2" works but the addition check fails.

It seems that when I find a way to fix one problem, another pops up.

Any suggestions will be greatly appreciated! Code follows


<SCRIPT language = JavaScript>

<!--
 var numbertotal = new Number;
function validateForm() {

//This part of the function validateForm checks to see if -2 is the
value entered.

  if (document.openp5.cexptot.value =="-2") {
   alert ("The data entered in question 77 is -2. Please enter a valid
number.");
   return false;}

  if (document.openp5.cbldgex.value =="-2") {
   alert ("The data entered in question 78 is -2. Please enter a valid
number.");
   return false;
   }

if (document.openp5.cbkmoex.value =="-2") {
   alert ("The data entered in question 79 is -2. Please enter a valid
number.");
   return false;
   }


  if (document.openp5.celecex.value =="-2") {
   alert ("The data entered in question 80 is -2. Please enter a valid
number.");
   return false;
   }

  if (document.openp5.celeqex.value =="-2") {
   alert ("The data entered in question 81 is -2. Please enter a valid
number.");
   return false;
   }

  if (document.openp5.cequipex.value =="-2") {
   alert ("The data entered in question 82 is -2. Please enter a valid
number.");
   return false;
   }

  if (document.openp5.cothex.value =="-2") {
   alert ("The data entered in question 83 is -2. Please enter a valid
number.");
   return false;
   }


//This part of the function validateForm checks the addition of fields.
A = Number(openp5.cbldgex.value);
B = Number(openp5.cbkmoex.value);
C = Number(openp5.celecex.value);
D = Number(openp5.celeqex.value);
E = Number(openp5.cequipex.value);
F = Number(openp5.cothex.value);

numbertotal1 = (A + B + C + D + E +F);
if (numbertotal1 != openp5.cexptot.value) {
 alert ("Question 77 has an incorrect total. Please check your
addition.");
 return false;}


}
// -->
</SCRIPT>


</head>

<body>

<h2>#name#</h2>

<p><b>NON RECURRING EXPENDITURES:</b> Report expenditures for the
acquisition of or additions
to fixed assets such as building sites, new buildings and building
additions, new equipment,
initial book stock, furnishings for new or expanded buildings, and new
vehicles.
Round all figures to the nearest dollar and do not
enter commas.

<form action="updatep5.cfm" method="post" name="openp5" onSubmit="return

validateForm(); add()">
<INPUT TYPE="hidden" NAME="sname" VALUE="#shortname#">
<INPUT TYPE="hidden" NAME="name" VALUE="#name#">



<table width="90%"  border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td><H3>77.&nbsp;TOTAL NON-RECURRING EXPENDITURES</H3></td>
    <td>&nbsp;</td>
    <td><input type="text" name="cexptot" size="10" maxlength="10"
VALUE="#numberformat(Trim(cexptot)'999999')#"

onFocus="this.select() ;"></td>
  </tr>

   <tr>
    <td>78.&nbsp;Building(s)</td>
    <td><input type="text" name="cbldgex" size="10" maxlength="10"
VALUE="#numberformat(Trim(cbldgex),'999999')#"

onFocus="this.select() ;"></td>
    <td>&nbsp;</td>
  </tr>


  <tr>
    <td>79.&nbsp;Bookmobile/Vehicles</td>
    <td><input type="text" name="cbkmoex" size="10" maxlength="10"
VALUE="#numberformat(Trim(cbkmoex)'999999')#"

onFocus="this.select() ;"></td>
    <td>&nbsp;</td>
  </tr>

<tr>
    <td>80.&nbsp;Electronic Access</td>
    <td><input type="text" name="celecex" size="10" maxlength="10"
VALUE="#numberformat(Trim(celecex)'999999')#"

onFocus="this.select() ;"></td>
    <td>&nbsp;</td>
  </tr>

<tr>
    <td>81.&nbsp;Equipment Electronic</td>
    <td><input type="text" name="celeqex" size="10" maxlength="10"
VALUE="#numberformat(Trim(celeqex)'999999')#"

onFocus="this.select() ;"></td>
    <td>&nbsp;</td>
  </tr>

<tr>
    <td>82.&nbsp;Furniture and Other Equipment</td>
    <td><input type="text" name="cequipex" size="10" maxlength="10"
VALUE="#numberformat(Trim(cequipex)'999999')#"

onFocus="this.select() ;"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>83.&nbsp;Other</td>
    <td><input type="text" name="cothex" size="10" maxlength="10"
VALUE="#numberformat(Trim(cothex)'999999')#"

onFocus="this.select() ;">
 </td>
    <td>&nbsp;</td>
  </tr>


Thank you!!




--
Cheryl Kirkpatrick
Web Administrator
South Carolina State Library
http://www.state.sc.us/scsl/
Email: cheryl at leo.scsl.state.sc.us
Telephone: 803.734.5831





More information about the Javascript mailing list