473,811 Members | 3,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is this considered a float?

Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(pric e) && parseFloat(pric e) 0)) {
alert("Price field cannot be empty or zero!");
return;
}

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}

Thanks, - Dave
Dec 3 '07 #1
5 1892
la***********@z ipmail.com wrote on 03 dec 2007 in comp.lang.javas cript:
Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(pric e) && parseFloat(pric e) 0)) {
alert("Price field cannot be empty or zero!");
return;
}
How do you define a float? Any noninteger number?

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}
Just:

if ( isNaN(price) || !price )
alert("Price field cannot be zero or nonnumeric!");

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 3 '07 #2
la***********@z ipmail.com wrote:
I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic.
....

return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
!(/[^0-9.]/).test(n) tests "Is there no character other than 0-9 and .?"
"29..99" passes this; all the characters are either 0-9 or a period.

(/\.\d/).test(n) tests "Is there a period followed by a digit?"
"29..99" passes this; there is a period followed by a dot.

--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Dec 3 '07 #3
la***********@z ipmail.com wrote:
I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic.
....

return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
!(/[^0-9.]/).test(n) tests "Is there no character other than 0-9 and .?"
"29..99" passes this; all the characters are either 0-9 or a period.

(/\.\d/).test(n) tests "Is there a period followed by a digit?"
"29..99" passes this; there is a period followed by a digit.

--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Dec 3 '07 #4
On Dec 4, 8:33 am, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(pric e) && parseFloat(pric e) 0)) {
alert("Price field cannot be empty or zero!");
return;
}

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}

Thanks, - Dave
If you want to validate that the user has entered something resembling
money in a particular format using a regular expression, then try:

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

A very simple RegExp test along lines of what you seem to want is:

function isMoneyFormat(s ) {
return /^\d+\.\d\d$/.test(s)
}

Which returns true for a string like 0.00 or 1.23 or 1231.23 and so
on.
--
Rob
Dec 4 '07 #5
In comp.lang.javas cript message <164a6bf5-383c-4c10-b02a-300aba0dda11@s1
9g2000prg.googl egroups.com>, Mon, 3 Dec 2007 14:33:31,
"la***********@ zipmail.com" <la***********@ zipmail.compost ed:
>I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(pric e) && parseFloat(pric e) 0)) {
alert("Price field cannot be empty or zero!");
return;
}
function isPrice(s) { return (/^\s*\d+(\.\d\d) ?\s*/$).test(s) && s>0 }
// Negative and exponent not allowed; well-formed by IUPAP/SUNAMCO
// integer euros or cents, as euros

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat
function isFloat(s) { return (/^\s*\d+(\.\d+)? \s*$/).test(s) }
// Negative and exponent not allowed; well-formed by IUPAP/SUNAMCO
// allows integer
function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}
function isInteger(s) { return (/^\s*\d+\s*$/).test(s) }
UNDERTESTED. Remember that all ECMA 3 Numbers are IEEE doubles, which
are floats.

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

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

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
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.
Dec 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1880
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X < float(Y) )?
13
1529
by: amanayin | last post by:
The program is simple and i know i should use switch instead of if but i done it this way for a reason. But i am not sure if it should work when i was compiling it i was getting warning messages but then suddenly it started working. The reason i wrote the program was to get used to using functions, if statement and logical operators. /* ARITH1.C SIMPLE CALCULATOR PROGRAM */ #include<stdio.h>
20
3150
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
12
2212
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
10
1355
by: rob.iverson | last post by:
We ran into this problem and I can't see why the compilers (Sun One Studio 8 and g++ 2.95) think it's an error: ------------------- class A { public: void foo();
8
6162
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
38
2308
by: Mark Dickinson | last post by:
I get the following behaviour on Python 2.5 (OS X 10.4.8 on PowerPC, in case it's relevant.) (0.0, 0.0) (-0.0, -0.0) I would have expected y to be -0.0 in the first case, and 0.0 in the second. Should the above be considered a bug, or is Python not expected to honour signs of zeros? I'm working in a situation involving complex arithmetic where branch cuts, and hence signed
24
2133
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
2
2554
by: kgeorge2 | last post by:
A library which i use, has an array class class LibArray { public: LibArray():_array(NULL),_len(0); LibArray(int n, float val):_array( new float),_len(n){} /** more complete definition omitted
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10394
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
9201
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
7665
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
6882
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
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.