Use Regular Expression to check dollar amount in JavaScript - Javascript RegExp

Javascript examples for RegExp:Match Number

Description

Use Regular Expression to check dollar amount in JavaScript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from www .  j a  v  a  2 s. c o m
var r = /^\$?[0-9]+\.?[0-9]?[0-9]?$/;
console.log(r.test("12.55"));  //true
console.log(r.test("$12.55"));  //true
console.log(r.test("$12."));    //true
console.log(r.test("$12"));     //true
console.log(r.test("$$12"));     //false
console.log(r.test("$12.555")); //false
console.log(r.test("12.5$55")); 
    });

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

Related Tutorials