I have a isNumeric function to check a text field is numeric .. I nowneed a isFloat

B

bizt

Hi,

I am using the following function to validate a forms value as an
integer

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}

I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks

Burnsy
 
J

Janwillem Borleffs

bizt schreef:
I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks

This could do it:

var numericExpression = /^[0-9]+(\.[0-9]+)?$/;


JW
 
J

Joost Diepenmaat

SAM said:
bizt a écrit :
I need one to validate a forms value as a float number.


function isNum(str) {
return str==str.match(/^[0-9]*\.?[0-9]*/);
}

That would match

""
".foo"
"0bar"
".9" (as far as I can see not technically a valid javacript number, but
works in at least spidermonkey)

and a lot of other non-numeric strings.

it would also not match the technically correct (in javascript, at
least):

"-0.2"
"2.3e4"
"4.4E5"
"+2"
"-3"

See Ecma 7.8.3 for the DecimalLiteral production.

If you don't want signs and no exponents, and allow .XXX as 0.XXXX
(but not 7. as 7) it would look something like

str.match(/^\d*(\.\d+)?$/)
 
D

Dr J R Stockton

In comp.lang.javascript message <24b65960-c3a8-444a-b5a9-9363924a40a9@c6
5g2000hsa.googlegroups.com>, Wed, 17 Sep 2008 08:37:23, bizt
I am using the following function to validate a forms value as an
integer

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}


That shows that the input is a non-empty string of decimal digits. The
number of possible integer values is one less than twice the number of
strings that the above allows.
I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks

All JavaScript Numbers are stored in float format.

Do you want to consider integers as a subset of floats?

How about fixed-point numbers? Two replies have provided only tests for
fixed-point - perhaps they don't know what floating-point really means..
Should you exclude infinities?

Should 33.0 be acceptable as an integer?

For a problem to be reliably solved, it is first necessary for it to be
accurately defined.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
C

Curtis

bizt said:
Hi,

I am using the following function to validate a forms value as an
integer

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}

I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks

Burnsy

Technically, if something is numeric, it shouldn't matter if it's a
float or not. However, you might try:

function isInteger(num) {
if ( !isNaN(num) ) {
return parseInt(num) == parseFloat(num) ? true : false;
}
else {
return false;
}
}

function isFloat(num) {
if ( !isNaN(num) ) {
if ( /\.0+$/.test(num) ) {
return true;
}
else {
return parseInt(num) != parseFloat(num) ? true : false;
}
}
else {
return false;
}
}

Guess I couldn't entirely eliminate regex.
 
S

SAM

Joost Diepenmaat a écrit :
SAM said:
bizt a écrit :
I need one to validate a forms value as a float number.

function isNum(str) {
return str==str.match(/^[0-9]*\.?[0-9]*/);
}

That would match

that matches a non relative number (with separator = '.')
""
".foo" no
O
".9"
yes and ?
(as far as I can see not technically a valid javacript number, but

since when ?
works in at least spidermonkey)

alert(2*.001); works in all my browsers since at least NC3
and a lot of other non-numeric strings.

it would also not match the technically correct (in javascript, at
least):

It was not asked.
"-0.2"
"2.3e4"
"4.4E5"
"+2"
"-3"

See Ecma 7.8.3 for the DecimalLiteral production.

If you don't want signs and no exponents, and allow .XXX as 0.XXXX
(but not 7. as 7) it would look something like

str.match(/^\d*(\.\d+)?$/)

'.05'.match(/^\d*(\.\d+)?$/) // -> .05,.05

The problem was not to extract a number but to know if a string could be
a number (expect decimal number)

Is it a relative number (eventually decimal one) :

function isNum(str) {
return str==str.match(/[-+]?[0-9]*\.?[0-9]*/);
}
 
J

Joost Diepenmaat

SAM said:
Joost Diepenmaat a écrit :
SAM said:
bizt a écrit :
I need one to validate a forms value as a float number.

function isNum(str) {
return str==str.match(/^[0-9]*\.?[0-9]*/);
}

That would match

that matches a non relative number (with separator = '.')
""
".foo"
no

Yes.

js> ".foo".match(/^[0-9]*\.?[0-9]*/)
..

Since when is 0bar a number, then?
yes and ?


since when ?


alert(2*.001); works in all my browsers since at least NC3


It was not asked.

I'm not so sure. The original post does not define what is meant by
"floating point number" OR "validate".
'.05'.match(/^\d*(\.\d+)?$/) // -> .05,.05

I know. That's what I said.
 
O

optimistx

Dr said:
For a problem to be reliably solved, it is first necessary for it to
be accurately defined.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

Yes, correct. If any poster defines the problem accurately and reads all of
c.l.j
(of course understanding everything) and FAQ (incl. ECMAScript-262
standards),
then most probably there is no need to ask. This might take some years,
though.

And that may be a good thing: no stupid questions here, only perfectly
well formulated expert questions, which will be answered perfectly and
reliably, with even all the spaces in correct places as in jslint.com in
strictest
mode.
 
D

Dr J R Stockton

Wed said:
If you don't want signs and no exponents, and allow .XXX as 0.XXXX
(but not 7. as 7) it would look something like

str.match(/^\d*(\.\d+)?$/)

That matches an empty string; the * should be a + .

For mere validation, .test seems slightly better than .match .

One should not allow a number to start or finish with a dot; it is too
likely to be a typo or an eyeball-o. A comma is also bad, for slightly
different reasons. There are good reasons for calling point and virgule
"decimal separators".

Note that the OP's definition (when provided) of allowable input may not
necessarily exactly match any construct in ISO/IEC 16262.
 
S

SAM

Joost Diepenmaat a écrit :
I'm not so sure. The original post does not define what is meant by
"floating point number" OR "validate".

Possibly the OP has so much difficulties with english as me ?

I refer only to proposed function

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}

function isNumeric(str) --> true/false

Understood this function has to be applied on text-fields of a form to
check if the values could be a number (in normal human design)
I expect the form is not submited if that find an "error"


function isNumeric(str) { return str == str*1; }
 
T

Thomas 'PointedEars' Lahn

Curtis said:
bizt said:
I am using the following function to validate a forms value as an
integer

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}

I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks

Technically, if something is numeric, it shouldn't matter if it's a
float or not. However, you might try:

function isInteger(num) {
if ( !isNaN(num) ) {
return parseInt(num) == parseFloat(num) ? true : false;
}
else {
return false;
}
}

That is not going to provide a correct result. Remember that parseInt()
without second argument parses the string based on its prefix: "0x" as
hexadecimal, "0" as octal (implementation-dependent; JavaScript 1.8 still
does it, though), other numerics as decimal, and non-numeric as NaN -- which
is the reason why we usually recommend parseInt(..., 10) in scripts.
parseFloat(), on the other hand, parses every value as a decimal value and
returns `NaN' if it cannot be interpreted as such. So for example

!isNaN("0123")

evaluates to `true', but

parseInt("0123") == parseFloat("0123")

evaluates to `false' (because 83 != 123), although the value clearly can be
considered an integer.

And finally, the `==' operation results in a boolean value already. The
conditional operation is superfluous and inefficient in such a case; that
would seem to apply for all programming languages that have it.
function isFloat(num) {
if ( !isNaN(num) ) {
if ( /\.0+$/.test(num) ) {
return true;
}
else {
return parseInt(num) != parseFloat(num) ? true : false;
}
}
else {
return false;
}
}

Obvious by now, this test is equally flawed. ISTM it can be replaced safely
with

function getDecimals(num)
{
return num % 1;
}

While the return value is of type `number' and not of type `boolean' here,
it suffices for implicit type conversion later. Incidentally, it does not
make much sense to test an ECMAScript Number value for being a
floating-point value, because *all* ECMAScript Number values are IEEE-754
double-precision [64-bit] floating-point values.
Guess I couldn't entirely eliminate regex.

Guess you haven't RTFM, the FAQ, or the Specification.


PointedEars
 
C

Curtis

Thomas said:
Curtis said:
bizt said:
I am using the following function to validate a forms value as an
integer

function isNumeric(str){
var numericExpression = /^[0-9]+$/;
if(str.match(numericExpression)){
return true;
}else{
return false;
}
}

I need one to validate a forms value as a float number. I understand
that it should just simply be a change to the value of the
numericExpression variable but Im not too good with expression. Ive
tried doing some searches on google but either couldnt find the
relevant function or they just didnt work. Can anyone help? Thanks
Technically, if something is numeric, it shouldn't matter if it's a
float or not. However, you might try:

function isInteger(num) {
if ( !isNaN(num) ) {
return parseInt(num) == parseFloat(num) ? true : false;
}
else {
return false;
}
}

That is not going to provide a correct result. Remember that parseInt()
without second argument parses the string based on its prefix: "0x" as
hexadecimal, "0" as octal (implementation-dependent; JavaScript 1.8 still
does it, though), other numerics as decimal, and non-numeric as NaN -- which
is the reason why we usually recommend parseInt(..., 10) in scripts.
parseFloat(), on the other hand, parses every value as a decimal value and
returns `NaN' if it cannot be interpreted as such. So for example

!isNaN("0123")

evaluates to `true', but

parseInt("0123") == parseFloat("0123")

evaluates to `false' (because 83 != 123), although the value clearly can be
considered an integer.

Yes, thanks, I forgot about that.
And finally, the `==' operation results in a boolean value already. The
conditional operation is superfluous and inefficient in such a case; that
would seem to apply for all programming languages that have it.

Yes, I see now, how inefficient I had it.
Obvious by now, this test is equally flawed. ISTM it can be replaced safely
with

function getDecimals(num)
{
return num % 1;
}

Very nice, that's way more elegant, and simpler.
While the return value is of type `number' and not of type `boolean' here,
it suffices for implicit type conversion later. Incidentally, it does not
make much sense to test an ECMAScript Number value for being a
floating-point value, because *all* ECMAScript Number values are IEEE-754
double-precision [64-bit] floating-point values.
Guess I couldn't entirely eliminate regex.

Guess you haven't RTFM, the FAQ, or the Specification.

I have read the manual, some of the FAQ, but not the spec. I wasn't
trying to come off as arrogant or anything, sorry if it sounded that
way. Thanks for the correction, I wouldn't want to mislead anyone with
an incorrect solution.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top