regex - Detect if string is number or has decimal value in javascript -


i need users enter amount , may come in different formats 500 or 500.00. need check if user has entered number or number 2 decimal points. far have tried

if(/^\d+$/.test(amount) === false || /[0-9]+(\.[0-9][0-9]?)?/.test(amount) === false){           //valid }else{          //invalid } 

but far 1 check if number working fine.

i guess looking this

var pattern=/^\d+(\.\d{2})?$/; if(pattern.test(amount)) {           //valid number pattern } else {          //invalid number pattern } 

\d+(\.\d{2})?$ match 1 many digits optionally followed 2 decimal values..


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -