R J. R J. - 2 years ago 173
jQuery Question

Allow only 2 decimal points entry to a textbox using jquery?

Allow only 2 decimal points when entering number to a textbox using jquery.

Please suggest any regex to allow only two decimal in textbox.

I have tried the following code.

$("#amountId").val().replace (/(\.\d\d)\d+|([\d.]*)[^\d.]/, '$1$2');

Answer Source

You could do it without regex:

var dec = parseFloat($("#amountId").val(),10).toFixed(2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download