soccer022483
04-25-2006, 02:20 AM
Ok i need a good regular expression to check for a price format. Price looks like:
65.50 or 65
decimal is optional. Anyone know?
65.50 or 65
decimal is optional. Anyone know?
|
||||
regular expression for pricesoccer022483 04-25-2006, 02:20 AM Ok i need a good regular expression to check for a price format. Price looks like: 65.50 or 65 decimal is optional. Anyone know? ralph l mayo 04-25-2006, 03:30 AM I think this works, it may not be the best: ^[0-9]+(\.[0-9]{2})?$ (translation: from the beginning (^) match only numbers for however many there are, at least one, optionally followed by a group containing a decimal and two more numbers before the end of the string ($)) chump2877 04-25-2006, 04:02 AM this also works: reg = /^\d+(\.\d{2})?$/; I see you beat me to it Ralph! :p Edit: just to see how it works wth different numbers: <HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <script type="text/javascript"> <!-- var string = new Array(); string[1] = "109.50"; string[2] = "19.50"; string[3] = "0.50"; string[4] = "19"; string[5] = "9.0.7"; string[6] = "word"; string[7] = "9.7"; string[8] = ".45"; reg = /^\d+(\.\d{2})?$/; for (x=1; x<=8; x++) { if (reg.test(string[x])) { document.write(string[x]+' is a valid price.<br>'); } else { document.write(string[x]+' is NOT a valid price.<br>'); } } --> </script> </BODY> </HTML> returns: 109.50 is a valid price. 19.50 is a valid price. 0.50 is a valid price. 19 is a valid price. 9.0.7 is NOT a valid price. word is NOT a valid price. 9.7 is NOT a valid price. .45 is NOT a valid price. |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum