[Javascript] I.E.: 1+1=1. Worse yet, Netscape: 1+1 =___

Peter Brunone peter at brunone.com
Wed Jun 27 17:45:24 CDT 2001


Good eye, Rodney...

    You've made the function more usable and the inclusion of the TotalHours
box should help keep things straight (I made a rather lazy assumption that
integers were enough).

    Two other suggestions:

    1)  Use onFocus="window.focus()" in the TotalHours field to keep people
from putting the cursor there (in IE you can even make the background gray
using style attributes).
    2)  Do a more general check for validity and tell the user to fix it
before they go on, e.g.

if((parseFloat(officeHours)) != officeHours) {
 alert("Your entry is invalid");
 f.Office_Hours___________.focus();
 }

    This will take care of multiple decimal points as well.

----- Original Message -----
From: "Rodney Myers" <rodney at aflyingstart.net>
To: <javascript at LaTech.edu>
Sent: Wednesday, June 27, 2001 5:12 PM
Subject: Re: [Javascript] I.E.: 1+1=1. Worse yet, Netscape: 1+1 =___


> I agree with Peter that onChange is the best event handler for text
> inputs.
>
> I would change all the handlers to
>  onChange="sumboxes(this.form)"
> AND include this handler in input Total_Hours__________________
> Why? Because you don't want to allow any change there!
>
> Not every input had an event handler, which explains some of your
> problems.
>
> Notice that the form object is being passed to your function and
> received with (f) in
>
> function sumboxes(f)
>
> Peter's construction is instructive but misses two points
>
> 1. parseInt does not allow for fractions of an hour [1.5] for example.
> 2. There is no allowance for the removal of non-numerics.
>
> Here is my version of sumboxes()
>
> function sumboxes(f) {
>  var totalHours = 0;
>
>  totalHours +=hfix(f.Prep_Hours___________________);
>  totalHours +=hfix(f.Office_Hours_________________);
>  totalHours +=hfix(f.In_Service_Hours_____________);
>  totalHours +=hfix(f.Lesson_Hours_________________);
>  totalHours +=hfix(f.Travel_Hours_________________);
>  totalHours +=hfix(f.Other_Hours__________________);
>
>  f.Total_Hours__________________.value = totalHours;
>  }
>
>
> And here are the support functions :
> The aims are to
> 1. Allow decimals
> 2. Clean up the element value
> 3. Return the remaining numerics as numerics
>
> function hfix(el){
>
> // AIM #2
> // cleans up any odd input in the form
> el.value=decimals(el.value);
>
> // el.value=units(el.value); // use this if decimals NOT permitted
>
> // AIM #3
> // multiplying by 1 forces string to numeric
> return(el.value.length==0 ? 0 : el.value * 1)
> }
>
> function units(str){ return(clean(str,"1234567890")) }
>
> // AIM #2
> function decimals(str){ return(clean(str,"1234567890.")) }
>
> function clean(str,permitted){
> str=""+str;
> var nchar,n;var out="";
> for(n=0;n<str.length;n++)
>  {
>  nchar=str.charAt(n);
>  out+=permitted.indexOf(nchar)>-1?nchar:"";
>  }
> return(out);
> }
>
> One weakness of my code is that it does not check for there being more
> than one decimal point.
>
>
> hth
>
> Rodney
>
>
>
> "McCoy, Thomas" wrote:
>
> >  Many thanks go to Peter Brunone for your help, however...I updated
> > the code, but things still don't "add up"   (okay, bad
> > joke). Whichever box I enter a number into first is never added to the
> > total... unless I go back and retype that number once addition has
> > begun.  Also, the script seems to be completely ignored by Netscape
> > 4.7. I tried setting an "initial value" of "0" for each form element,
> > hoping that a change from zero would trigger a response from the
> > JavaScript.  It did not. The newer version of the form can be tested
> > at http://www.city.newport-beach.ca.us/nbpl/literacyform04.htm.
> > Thanks again for your expertise and time!





More information about the Javascript mailing list