Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Numneric Validation


Message #1 by "Harish Margabandhu" <harish_marg@f...> on Mon, 23 Dec 2002 19:57:42
	What level of intelligence do you need ?  If you don't need
real-time checking (ie validate at every user's keypress), here're the
logical steps :
1) parseFloat your data value and check if it returns error (like NaN). 
 If
ok, keeps the result in a variable (called a for convenience)
2) multiply a by 10
3) use Math.floor(a) to truncate decimal part, which is in fact 
everything
after the 2nd digit after the decimal for the original value as you 
wanted
4) check if the value is between 0 and 999999
5) divide the result of 4) by 10 to get back the value needed
6) usually and ideally, you could use a.toFixed(1) to put the result 
back as
a string, however, unfortunately, there's a very very stupid bug in IE 
which
prevents values between 0.5 and 0.94 to work properly.  You could check
recent posts in the mailing-list to know more.  Luckily, since your
requirement is very regular, you could do like this :

var str =3D Math.floor(a) + '.' + ((a - Math.floor(a) * 10);

> -----Message d'origine-----
> De : Harish Margabandhu [mailto:harish_marg@f...]
> Envoy=C3=A9 : lundi 23 d=C3=A9cembre 2002 20:58
> =C3=80 : javascript
> Objet : [javascript] Numneric Validation
>
>
> Hi
>
> I require to validate a numeric value ranging from 0.0 to 99999.9 in
> javascript, also when the user keys in more than 1 digit after the
> decimal, other digits should ne truncated or ignored.
> Example if user keys in 99.98 it should be truncated to 99.9.
> No rounding
> should ne done as it would mis lead the user.
>
> If anybody have a logic please get some suggestions/code
>
> Thanks
> Harish M

  Return to Index