...

View Full Version : regular expression for currency (like 345.99)



ventura
05-20-2003, 12:46 AM
i want to make sure that a user enters a number (currency) in the format of 123.45 where there is no dollar sign but there will always be at least 1 number before the decimal and only 2 numbers after the decimal.

how do i do this using regular expressions?

cheesebagpipe
05-20-2003, 01:06 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var oRegExp = /^\s*\d+\.\d{2}\s*$/;

x = '2.99';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = '322.99';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = '2.9';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = '.99';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = '0.99';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = '299';
alert('"' + x + '" --> ' + oRegExp.test(x));
x = ' 29.88 ';
alert('"' + x + '" --> ' + oRegExp.test(x));

</script>
</head>
<body>
</body>
</html>


You can remove the two \s* if you don't want to permit whitespace on either side of the entry.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum