[Javascript] newbie question

William T. Simmons tsimmons at employmentlawadvisors.com
Thu Nov 7 13:46:14 CST 2002


Cheryl,
This is how my typical scripting sessions go (changes, new stuff, more
changes) - why not simplify things even further and get rid of the hidden
field "total"? It's not needed - also, maybe it would be better to just call
the function "verify()", since its only purpose is to verify the number
given in the "Total Staff:" field. Finally, to ensure that all variables and
values are in number format, use "Number()", as you had in your original
example:
function verify() {
 a = Number(openp2.ALA1FT.value);
 b = Number(openp2.MLS1FT.value);
 if ((a + b) != Number(openp2.STAFF1FT.value)) {
  alert ("Check your addition");
 }
 else alert("Thank you.");
}

Tommy Simmons
Employment Law Advisory Network
www.employmentlawadvisors.com

----- Original Message -----
From: "William T. Simmons" <tsimmons at employmentlawadvisors.com>
To: <javascript at LaTech.edu>
Sent: Thursday, November 07, 2002 1:11 PM
Subject: Re: [Javascript] newbie question


> Cheryl,
> I'm back on my regular e-mail system and want to give you a simplification
> of the code I furnished earlier - consolidate the "add()" and "verify()"
> functions and just have the one:
> function add() {
>  A = openp2.ALA1FT.value;
>  B = openp2.MLS1FT.value;
>  openp2.total.value = (A + B);
>  if (openp2.total.value != openp2.STAFF1FT.value) {
>   alert ("Check your addition");
>  }
> }
>
> It's not that big a deal, but it does simplify the code a bit.
>
> Tommy Simmons
> Employment Law Advisory Network
> www.employmentlawadvisors.com
>
> ----- Original Message -----
> From: <wallylaw at yahoo.com>
> To: <javascript at LaTech.edu>
> Sent: Wednesday, November 06, 2002 10:36 PM
> Subject: Re: [Javascript] newbie question
>
>
> > Cheryl,
> > You've probably already gotten some solutions (I can't check my e-mail
> > via my regular account), but in case you haven't heard back on this,
> > this should work for you:
> > < script language="JavaScript" > < !--
> > function add() {
> > A = openp2.ALA1FT.value;
> > B = openp2.MLS1FT.value;
> > openp2.total.value = (A + B);
> > verify();
> > }
> >
> > function verify() {
> > if (openp2.total.value != openp2.STAFF1FT.value) {
> > alert ("Check your addition.");
> > }
> > }
> > / /-- > < /script>
> >
> > The last line in your form code should omit the call to "verify()",
> > since that is taken care of by the call to "add()":
> > < input type=button name= b1 value="Submit data" onClick = add(); >
> >
> > HTH,
> > Tommy Simmons
> > Employment Law Advisory Network
> > www.employmentlawadvisors.com
> >
> > --- Cheryl Kirkpatrick <cheryl at leo.scsl.state.sc.us> wrote:
> > > I am trying to use Javascript to verify the addition in an online
> > > form.
> > > My boss wants the user to supply the sum of several numbers and than
> > > have the form verify the sum. I am trying to get a simple version
> > > working before I plug it in my form. The addition works fine but I
> > > can't
> > > get the If then statement to work.
> > >
> > > Can you tell me what I am doing wrong?
> > >
> > >
> > > function add() {
> > > A = document.openp2.ALA1FT.value
> > > B = document.openp2.MLS1FT.value
> > > A = Number(A)
> > > B = Number(B)
> > > C = (A + B)
> > > document.openp2.total.value = C
> > >
> > > }
> > > function verify() {
> > > If (document.openp2.total.value != 'STAFF1FT');
> > > alert ("Check your addition");
> > > }
> > >
> > > </SCRIPT>
> > >
> > > </head>
> > >
> > > <body>
> > >
> > > <FORM name="openp2">
> > > l<input type="hidden" name="total" size="3" maxlength="3">
> > >
> > > <p>full time librarians: <input type="text" name="ALA1FT" size="3"
> > > maxlength="3" value=""></p>
> > >
> > > <p>part time librarians: <input type="text" name="MLS1FT" size="3"
> > > maxlength="3" value=""></p>
> > >
> > >
> > > Total staff: <input type="text" name="STAFF1FT" size="3"
> > > maxlength="3">
> > >
> > >
> > > <p>
> > >
> > > <input type=button name= b1 value="Submit data" onClick = add();
> > > verify()>
> > >
> > > </p>
> > >
> > >
> > > </form>
> > >
> > > Thank you in advance!
> > > --
> > > Cheryl Kirkpatrick
> > > Web Administrator/
> > > Information Technology Librarian
> > > South Carolina State Library
> > > http://www.state.sc.us/scsl/
> > > Telephone: 803.734.5831
> >
> > __________________________________________________
> > Do you Yahoo!?
> > U2 on LAUNCH - Exclusive greatest hits videos
> > http://launch.yahoo.com/u2
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>





More information about the Javascript mailing list