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

Winapi c++: DialogBox hangs when breaking a loop -

vb.net - Font adding using PDFsharp -

javascript - jQuery iScroll clickable list elements while retaining scroll? -