473,794 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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
numericExpressi on 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 4164
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
numericExpressi on 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 numericExpressi on = /^[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.n lwrites:
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.javas cript message <24b65960-c3a8-444a-b5a9-9363924a40a9@c6
5g2000hsa.googl egroups.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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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
numericExpress ion 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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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
numericExpressi on 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************ *********@wanad oo.fr.invalidwr ites:
>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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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
numericExpress ion 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("012 3")

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 bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.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 numericExpressi on = /^[0-9]+$/;
if(str.match(nu mericExpression )){
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
numericExpres sion 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("012 3")

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
1517
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 numeric" else Response.Write strTemp & " is not numeric" end if
8
2324
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 following function in a validating class to use when needed. (Value = H880118A gave the error (like other 'unconvertible' strings)) Public Function Numeric(ByVal value) As Boolean
2
2014
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 now are not. When I use the "IsNumeric" function, it returns false. When I try to Ctype it to an integer, I get an "overflow" error. If I change the datatype of the database field to "int", it works fine. Whats the deal? The values I'm working with...
14
40146
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
1967
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 to solve the error.
10
2799
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 one and doesn't run an another one. The program test is very simple, just a form with a text box and a button. The text box has a validation rule that check if the insert string is a numeric value, that's all.
12
3064
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 e) if (e.NewValues != "" && Not IsNumeric(e.NewValues)) {
2
1543
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 IsNumeric(txtInput.Text) = True Then MessageBox.Show("Numeric entry found")
17
2299
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
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.