473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
9 4138
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steven Burn | last post by:
Anyone have any why the following, returns true? '// Leave everything after the ? strTemp = StripQStr("/?DD0898SDFSDFSD89Q2ASD8822") If IsNumeric(StrTemp) Then Response.Write strTemp & " is...
8
by: eje | last post by:
IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the following error message: "Input string was not in a correct format." I use the...
2
by: Glenn Venzke | last post by:
For some reason all of a sudden number values pulled from a SQL server field with a datatype of "numeric" are not being recognized by asp classic as valid number values. They were previously, but...
14
by: Kenny | last post by:
Hello, I would like to know if the function IsNumeric requires a header like #include <iostream> to be functionnal thanks ken
3
by: Radith Silva | last post by:
Dear All; Thanx for helpt with previous question. Still learning?? FROM A VB 6.0 BOOK: any way; I have used IsNumeric function and check all namespace conflicts and all and nothing seems...
10
by: michele | last post by:
I have a problem with VB.Net and IsNumeric() function because it always returns FALSE even the string can be a number. There is another strange thing, the same program (it is a test) runs good on...
12
by: sck10 | last post by:
Hello, I am trying to determine if a value is NOT numeric in C#. How do you test for "Not IsNumeric"? protected void fvFunding_ItemInserting_Validate(object sender, FormViewInsertEventArgs...
2
by: James | last post by:
Simple question... If you execute a block of statements that uses the IsNumeric function on a text box that contains no data, will it cause a run time error? Thanks looks like this: If...
17
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.