Allow only numbers and single decimal into inputbox using jquery

  function onlyDecimal(element, decimals) {
                $(element).keypress(function (event) {
                    var num = $(this).val();
                    num = isNaN(num) || num === ” || num === null ? 0.00 : num;
                    if ((event.which != 46 || $(this).val().indexOf(‘.’) != -1) && (event.which < 48 || event.which > 57)) {
                        event.preventDefault();
                    }
                    if ($(this).val() == parseFloat(num).toFixed(decimals)) {
                        event.preventDefault();
                    }
                });
            }
            onlyDecimal($(‘.singledecimalandnumber’), 2);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s