Floating Point Verification

0

00steve

Hi,
I am looking for tips on how to go about creating a function verifies
floating point input from a text box. For the sake of simplicity I
would like the function the take 3 parameters: the scale, precision
and error msg given if the entered value exceeds the max length
dictated by the scale and precision, or if non-numeric input is
provided.

As I have never done anything quite like this with javascript before,
I was wondering if anyone had any tips on what functions etc. would
come in handy.

Thanks for any input!
 
R

RobG

00steve said:
Hi,
I am looking for tips on how to go about creating a function verifies
floating point input from a text box. For the sake of simplicity I
would like the function the take 3 parameters: the scale, precision
and error msg given if the entered value exceeds the max length
dictated by the scale and precision, or if non-numeric input is
provided.

As I have never done anything quite like this with javascript before,
I was wondering if anyone had any tips on what functions etc. would
come in handy.

Thanks for any input!

Search for a thread called "isInteger, isFloat functions?" about
20-sep-04

From a Mike Winter post:
/^(0|[1-9])\d*$/

ensures that the string contains an integer with no leading zeros.

/^(-|+)?(0|[1-9])\d*$/

allows for signed integers.

To check for basic real numbers, it can be modified to:

/^(0|[1-9])\d*(\.\d+)?$/

which allows integer or real number inputs. To force decimal places and
to limit their number again, only simple modification is necessary:

/^(0|[1-9])\d*\.\d+$/ // must be real
/^(0|[1-9])\d*\.\d\d$/ // must be real with two decimal places
/^(0|[1-9])\d*\.\d{1,5}$/ // must be real with at most five d.p.

Scientific notation:

/^(0|[1-9])\d*\.\d+e(-|+)?\d+$/i

Optional scientific notation:

/^(0|[1-9])\d*\.\d+(e(-|+)?\d+)?$/i

Optional scientific notation with positive and negative numbers:

/^(-|+)?(0|[1-9])\d*\.\d+(e(-|+)?\d+)?$/i

Mandatory canonical scientific notation:

/^(-|+)?[1-9]\.\d+e(-|+)?[1-9]\d*$/i

Hexadecimal:

/^0x[0-9a-f]+$/i

[Note: these are untested]

Another, slightly modified method to just test for int or float. It
depends on the converted number being the same as the input, so it is
quite simple and flexible:
var x = someValue;
if (x == parseInt(x,10) && x == parseFloat(x)) {
alert(x + ' is a int');
} else if (x == parseFloat(x)) {
alert(x + ' is a float');
} else {
alert(x + ' is not a number I like...');
}

You can even use scientific notation if you like, so 1.2e+03 is
recognised as a float quite happily without complex pattern
matching.

Have fun
 
M

Michael Winter

[snip]
Search for a thread called "isInteger, isFloat functions?"
about 20-sep-04

From a Mike Winter post:

I did make a rather late correction (35 days) to that post. Occurances of

(0|[1-9])\d*

should be replaced with

(0|[1-9]\d*)

[snip]

I didn't post that: Fred Oz did.

[snip]

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 11 Dec 2004 09:49:52, seen in
00steve said:
I am looking for tips on how to go about creating a function verifies
floating point input from a text box. For the sake of simplicity I
would like the function the take 3 parameters: the scale, precision
and error msg given if the entered value exceeds the max length
dictated by the scale and precision, or if non-numeric input is
provided.

Please proof-read your articles before posting them.

Your question does not really make sense; presumably you are not a
mathematician nor a physical scientist. It would be better to explain
the true requirements.

There is no relevant maximum length for a floating-point number; any
number of leading zeroes are allowed in mantissa and exponent, without
affecting the value.

For a floating-point quantity, the first thing to do is to remove any
leading or trailing characters that you wish to allow but that might
affect the remaining code. Perhaps the only reasonable case would be
where these characters give units, in which case they will need to be
parsed and used. Leading/trailing spaces can be removed, but don't need
to be.

Let the text box be F.X0 ; then the best move seems to be to do
x = +F.X0.value first. You will normally have a range of allowed
values, given by a least L and a greatest G. The following should
suffice :-
L = 3 ; G = 9
x = +F.X0.value
OK = x >= L && x <= G
which, if I am not mistaken, correctly handles NaNs and Infs.

If you insist that the number is given in floating-point notation, then
test for the presence of the letter E. You might also wish to test for
the presence of a decimal point, and, if so, that it has a digit on each
side in accordance with recommended technical practice.

If you have other needs, please explain them better.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top