Karen Karen - 2 months ago 16
Javascript Question

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


Answer

Use this regex it will work

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

var num2 = '12345.6'
console.log(regex.test(num));  // False

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