473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate that dollar amount has no "cents"

I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on how I can do
this? Thanks

Mar 12 '06 #1
9 5821
bd***@hotmail.c om wrote:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on how I can do
this? Thanks


Math.round(Num) +".00"

But what do you do with $2.05 or $2.50?
Mar 12 '06 #2

"Kien" <ca*********@gm ail.com> wrote in message
news:Bd******** *********@news-server.bigpond. net.au...
bd***@hotmail.c om wrote:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on how I can do
this? Thanks


Math.round(Num) +".00"

But what do you do with $2.05 or $2.50?

-
Or
Math.floor(num) - to ignore anything after the decimal.
the $ make it NaN
Mar 12 '06 #3
Hal Rosser said the following on 3/12/2006 12:37 AM:
"Kien" <ca*********@gm ail.com> wrote in message
news:Bd******** *********@news-server.bigpond. net.au...
bd***@hotmail.c om wrote:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on how I can do
this? Thanks

Math.round(Num) +".00"

But what do you do with $2.05 or $2.50?

-
Or
Math.floor(num) - to ignore anything after the decimal.
the $ make it NaN


Neither of which limits the input to all digits, which is what the OP
wanted.

if (+fieldRef.valu e = parseInt(fieldR ef.value,10))

But a Regular Expression might be preferable.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 12 '06 #4
Yeah not sure how I really wanted to handle it, I figured regular
expressions may the way to go. I really didn't want them to be able to
type anything other than a say 2.00 or 2 or 4.00 or 5 etc... I wanted
to check it on keyup to make sure it meets the requirement. I'll mess
with with some of these. Let me know if you that clears up any. Thanks!

Mar 12 '06 #5
Looks like I can make randy's parses anything to the right of the
decimal. Is there I can just check to see if they have any cents and
alert them to fix it? Say if they have 2.25 then alert("Sorry has to
be in dollar amounts") I could just parse it like randy has but I'd
like to alert them that it isn't in the proper format and let them fix
it. Thanks

Mar 12 '06 #6
Got it I think:

var NumericRegExp =/^\d+(\.[0]{0,2})?$/;
var regex = new RegExp(NumericR egExp);
if (!regex.test(a) ){
alert(a + " is not a valid for this field.");
return false;
}
else {
alert("Ok");
}

Mar 12 '06 #7
bd***@hotmail.c om wrote:
Got it I think:

var NumericRegExp =/^\d+(\.[0]{0,2})?$/;
var regex = new RegExp(NumericR egExp);
if (!regex.test(a) ){
alert(a + " is not a valid for this field.");
return false;
}
else {
alert("Ok");
}


Just a thought: ask for whole dollars and check that the input is all
digits (i.e. an integer), e.g.

<label for="Amount">Pl ease enter whole dollars:&nbsp<b r>
<b>$</b><input type="text" id="Amount" onkeyup="
var msg = '';
if(!/^\d*$/.test(this.valu e)) msg = 'Whole $ only please!';
document.getEle mentById('msg') .innerHTML = msg;
}
"></label><div id="msg"></div>

--
Rob
Mar 12 '06 #8
Thanks rob I'll try that...

Mar 13 '06 #9
JRS: In article <11************ **********@i40g 2000cwc.googleg roups.com>
, dated Sun, 12 Mar 2006 11:36:01 remote, seen in
news:comp.lang. javascript, bd***@hotmail.c om posted :
Got it I think:

var NumericRegExp =/^\d+(\.[0]{0,2})?$/;
var regex = new RegExp(NumericR egExp);
if (!regex.test(a) ){
alert(a + " is not a valid for this field.");
return false;
}
else {
alert("Ok");
}


That above allows 0 0. 0.00 the middle of which is poor practice and
not included in what you asked for. Decimal points, being little,
should always be guarded by at least one digit in each side.

OK = /^\d+(\.00)?$/.test(a)

I prefer the structure
OK = <test>
if (!OK) <complain>
return OK

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 13 '06 #10

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

Similar topics

3
3248
by: Mensan | last post by:
I am trying to compute the differnece between two dwords in visual basic and keep getting the wrong value being computed. I have the following structure defind: Public Type HighLowQuote TradeDate As String OpenPrice As Double HighPrice As Double LowPrice As Double ClosePrice As Double
14
2685
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". Essentially, the smart guarantee promises to clean up resources whose ownership is passed into the function, for whatever defintion of "clean up" is most appropriate for the resource passed. Note that this is different from both the basic and the...
11
4771
by: Der Andere | last post by:
What exactly is the sense of using a "const" modifier before a function declaration? Actually, you can use it in three places. For instance, take a look at the following function declaration (from the introductory book "Absolute C++" by W. Savitch, p. 315) class Money { Money(); const Money operator + (const Money& amount2) const; private:
10
2061
by: Alf P. Steinbach | last post by:
The fifth part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been reviewed at all -- comments welcome! "How to use libraries" <url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_05.doc> General URL: <url: http://home.no.net/dubjai/win32cpptut/html/>
134
7914
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
7
8754
by: mrwoopey | last post by:
I have a asp.net application that uses the crystal report viewer control. It displays all of my reports but all of a sudden it started giving me the following error on ONLY two reports: "Fail to render the page" I can view the reports with within Crystal Reports. The same issue was posted here with no solution:
30
2914
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for some help to find out how the same thing is done in Python. I am not sure what the standard is. In C++, I open up an editor in one window, a Unix shell in another. I write the code in the editor, then switch to the shell window for compile...
3
7566
by: David Lozzi | last post by:
Howdy, In my GridView i have some bound columns displaying currency. I have DataFormatString="${0:C2}" in one of the columns but the value being displayed is $35 . If the price has cents, then it will display the cents, like $35.3 or $35.55, but where there's no cents, it just displays $35. Shouldn't this override any datatypes from the datasource. Just to cofirm I am sending a valid datatype, I checked everything out and this gridview...
4
2024
by: 511475 | last post by:
Originally Posted by 511475 this program is supposed to give the user the ability to enter numbers in cents and tell you how many dollars you have. Please anybody can tell me what's wrong. been working on it two weeks <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Cents to Dollars</title>
0
9595
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...
0
9432
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,...
1
10008
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
9873
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
8891
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
7420
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
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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.