473,386 Members | 1,720 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.

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 5786
bd***@hotmail.com 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*********@gmail.com> wrote in message
news:Bd*****************@news-server.bigpond.net.au...
bd***@hotmail.com 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*********@gmail.com> wrote in message
news:Bd*****************@news-server.bigpond.net.au...
bd***@hotmail.com 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.value = parseInt(fieldRef.value,10))

But a Regular Expression might be preferable.

--
Randy
comp.lang.javascript 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(NumericRegExp);
if (!regex.test(a)){
alert(a + " is not a valid for this field.");
return false;
}
else {
alert("Ok");
}

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

var NumericRegExp =/^\d+(\.[0]{0,2})?$/;
var regex = new RegExp(NumericRegExp);
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">Please enter whole dollars:&nbsp<br>
<b>$</b><input type="text" id="Amount" onkeyup="
var msg = '';
if(!/^\d*$/.test(this.value)) msg = 'Whole $ only please!';
document.getElementById('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**********************@i40g2000cwc.googlegroups .com>
, dated Sun, 12 Mar 2006 11:36:01 remote, seen in
news:comp.lang.javascript, bd***@hotmail.com posted :
Got it I think:

var NumericRegExp =/^\d+(\.[0]{0,2})?$/;
var regex = new RegExp(NumericRegExp);
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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
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...
14
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". ...
11
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...
10
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 -- ...
134
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...
7
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...
30
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...
3
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...
4
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.