Restrict textbox up to two decimals using JavaScript

 function twodecimals(event, obj) {

    var $this = obj;

    if ((event.which != 46 || $this.value.indexOf('.') != -1) &&

           ((event.which < 48 || event.which > 57) &&

           (event.which != 0 && event.which != 8))) {

        event.preventDefault();

    }

    var text = $this.value;

    if ((event.which == 46) && (text.indexOf('.') == -1)) {

        setTimeout(function () {

            if ($this.value.substring($this.value.indexOf('.')).length > 2) {

                $this.value($this.value.substring(0, $this.value.indexOf('.') + 2));

            }

        }, 1);

    }

    if ((text.indexOf('.') != -1) &&

        (text.substring(text.indexOf('.')).length > 2) &&

        (event.which != 0 && event.which != 8) &&

        ($this.selectionStart >= text.length - 2)) {

        event.preventDefault();

    }

}

0 Comment "Restrict textbox up to two decimals using JavaScript"

Post a Comment

Thank you for your comments