Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regular expression for currency (like 345.99)

    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?
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •