Wednesday 24 January 2018

Validate Only Decimal Number (with one dot) using Jquery.

Example : 12993.908 // valid
:  1212..99 // will not accept this.
 $(document).ready(function () {
 ValidDecimalNum();
 });
function ValidDecimalNum() {
            //Disable part of page
            $('.decimalNum').bind('cut copy paste', function (e) {
                e.preventDefault();
            });
            $('.decimalNum').bind("keypress", function (e) {
                var keycode = (e.which) ? e.which : e.keyCode;
                if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
                    return false;
                }
                else {
                    var parts = $(this).val().split('.');
                    if (parts.length > 1 && keycode == 46)
                        return false;
                    return true;
                }
            });
        }


If you are using asp.net update panel then in case of any postback you need to add you jquery function as below-

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
            function EndRequestHandler(sender, args) {
                ValidDecimalNum();
            }