Skip to content Skip to sidebar Skip to footer

Javascript Regex For Decimals

In Javascript, I am trying to validate a user input to be only valid decimals I have the following JSFiddle that shows the regex I currently have http://jsfiddle.net/FCHwx/ var reg

Solution 1:

This should be work

var regex = /^\d+(\.\d{1,2})?$/i;

Solution 2:

Have you tried this?

var regex = /^[0-9]+\.[0-9]{0,2}$/i;

Solution 3:

I recommend you to not use REGEX for this, but use a simple !isNaN:

console.log(!isNaN('20.13')); // trueconsole.log(!isNaN('20')); // trueconsole.log(!isNaN('20kb')); // false

Solution 4:

Try this:

\d+(\.\d{1,2})?

d is for digit d{1,2} is for 1 digit before . and at least 2 digits such as 0.51

Post a Comment for "Javascript Regex For Decimals"