Wednesday 25 December 2013

add 2 number before dot and after dot using regex using JQuery

I had a query that I need to validate input field which contain decimal value but should have only two values(number only) before decimal and after decimal using JQuery. and here is the working code

//Mask Float using jQuery
        $('.number').keypress(function (e) {
            var code = e.charCode || e.keyCode;
            var newValue = $(this).val() + String.fromCharCode(code);
            //debugger;
            var reg = /^\d{0,2}(\.\d{0,2})?$/;
            if (reg.test(newValue) == false) {
                e.preventDefault();
                return false;
            }
        });

No comments :

Post a Comment