0

The below code is to validate any input of decimal type with a precision of 2.

function check() {
    var str = $('#txttest').val();
    var patt = new RegExp("^[0-9]+(\.[0-9]{1,2})?$");
    var res = patt.test(str);
    alert(res);
}

Valid examples:

  • 12

  • 12.00

  • 12.00a

  • 12a

  • a12

  • 1a2.00

Failed Case:

In some cases, the function is returning wrong values, like 1a2.

Now, please suggest.

Sagnik Mukherjee
  • 163
  • 1
  • 1
  • 9

1 Answers1

0

Why don't you use JavaScript's toFixed() method?

Razvan Zamfir
  • 4,209
  • 6
  • 38
  • 252