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

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -