By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Sep 17 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Sep 17 '08 #2

P: n/a
SAM
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]*/);
}

--
sm
Sep 17 '08 #3

P: n/a
Joost Diepenmaat <jo***@zeekat.nlwrites:
See Ecma 7.8.3 for the DecimalLiteral production.
Oops: that should be Ecma-262, chapter 7.8.3

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Sep 17 '08 #4

P: n/a
In comp.lang.javascript message <24b65960-c3a8-444a-b5a9-9363924a40a9@c6
5g2000hsa.googlegroups.com>, Wed, 17 Sep 2008 08:37:23, bizt
<bi******@yahoo.co.ukposted:
>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) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 17 '08 #5

P: n/a
bizt wrote:
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.

--
Curtis
Sep 18 '08 #6

P: n/a
Dr J R Stockton wrote:
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.

Sep 18 '08 #7

P: n/a
SAM
Joost Diepenmaat a écrit :
SAM <st*********************@wanadoo.fr.invalidwrite s:
>It was not asked.

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; }

--
sm
Sep 18 '08 #8

P: n/a
Curtis wrote:
bizt wrote:
>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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 18 '08 #9

P: n/a
Thomas 'PointedEars' Lahn wrote:
Curtis wrote:
>bizt wrote:
>>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.
>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;
}
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.
>
PointedEars
--
Curtis
Sep 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.