[Javascript] combining if statements

Cheryl Kirkpatrick cheryl at leo.scsl.state.sc.us
Fri Feb 7 09:21:08 CST 2003


I am turning to this list again. I can not thank you enough for all the
help you have given me on my current project.

I am validating mathematics from a form. I had it working and then my
boss changed the rules for me. The form has to accept NR,nr, NA, na and
-1 as valid input. The "-1" is not a true -1 but rather is a symbol for
a unknown value. For purposes of validating addition and subtraction the
NR,nr, NA, na and -1 need to have the value of 0.

Separately I can make the NR,nr, NA, na work using:

if (document.openp5.books1beg.value =='NA') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='na') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='NR') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='nr') {
   K =Number(0);
   }
if (isNaN(openp5.books1beg.value) != true)
K = Number(openp5.books1beg.value);

L = Number(openp5.books2add.value);
M = Number(openp5.books3weed.value);

numbertotal4 = ((K + L) - M);
if (numbertotal4 != openp5.books4tot.value) {
 alert (" Line 90, column 1 has an incorrect total.
To get this figure, add Lines 87 and 88 and
then subtract Line 89.");
 return false;



Separately I can make the -1 work using:
if ((openp5.books1beg.value =="-1") == true) {
    K =Number(0);
   }

But when I combine the codes, they don't work. I am sure that I am not
combining them correctly but I have tried many versions and am starting
to pull my hair out! I think that my code is probably bloated and could
be shortened but at this point I need it to work. Any help is greatly
appreciated! !  Thank you!
Combined code follows.

if (document.openp5.books1beg.value =='NA') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='na') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='NR') {
   K =Number(0);
   }
if (document.openp5.books1beg.value =='nr') {
   K =Number(0);
   }
if ((openp5.books1beg.value =="-1") == true) {
    K =Number(0);
   }
if (isNaN(openp5.books1beg.value) != true)
K = Number(openp5.books1beg.value);

L = Number(openp5.books2add.value);
M = Number(openp5.books3weed.value);

numbertotal4 = ((K + L) - M);
if (numbertotal4 != openp5.books4tot.value) {
 alert ("Wrong! Line 90, column 1 has an incorrect
total. To get this figure, add Lines 87 and 88 and
then subtract Line 89.");
 return false;

--
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