View Full Version : verifying that an input is in currency format
ventura
10-15-2002, 12:54 AM
how can i use regular expressions to validate that someone has entered something in dollars and cents format?
right now i'm using:
var moneyExp = /\d+\.\d{2}/;
but it doesn't work if i enter in 1234.000000
thanks,
ed.
whammy
10-15-2002, 01:25 AM
Check this out:
http://www.siteexperts.com/tips/functions/ts23/demo.asp
Put in some currency value and some non-currency value and click the "validate" button. :)
Of course you can get her script from the source (if you download the separate .js file) to get the appropriate regex, but I would give her credit for sure, since I know a LOT of people that have learned from her work (and I'm one of them).
P.S. Of course your input won't validate (nor will it validate against Karen Gayda's) since what you've input doesn't match the regular expression you posted. Basically it has 2 extra digits after the decimal point whereas you have specified that the regex match ONLY two digits after the decimal... if you need to round a floating point integer off to two decimal places, there are other ways you can do that before validating against the regex... which I suspect is the actual problem.
:D
P.S. Nice client list there! :)
adios
10-15-2002, 01:40 AM
Need to specify (anchor) the ends of that pattern:
var moneyExp = /^\d+\.\d{2}$/;
whammy
10-15-2002, 01:44 AM
That's true too! But his input still won't match that regex... nor will it allow for the "$" character - but that's another post or three, or a good study of Karen's examples. P.S. this is another REALLY good link:
http://developer.netscape.com/docs/manuals/communicator/jsguide/regexp.htm
;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.