Skip to content Skip to sidebar Skip to footer

Javascript Regex For Decimal

I need a regex for a 5 digit integer number with 2 decimals. These would be correct: 12345.12 09856.99 45123.00 These wouldn't: 123.12 12345.6 98652

Solution 1:

Use this regex it will work

var regex = /^\d{5}\.\d{2}$/;
var num = '23445.09';
console.log(regex.test(num));  // Truevar num2 = '12345.6'console.log(regex.test(num));  // False

Demo : https://jsbin.com/wasefa/edit?html,js,console,output

Solution 2:

This would be correct : /^\d{5}\.\d{2}$/

var value = 12345.12;
value.toString().match(/^\d{5}\.\d{2}$/); // ['12345.12']var value = 98652;
value.toString().match(/^\d{5}\.\d{2}$/); // null

Post a Comment for "Javascript Regex For Decimal"