Regular match, how to keep two decimal places

js Code

var old = "";

        function check(ele) {
            var pattern = /^(\+|\-)?((([1-9]([0-9]*))|0)(\.[0-9]*)?)?$/;
            var val = ele.value;
            if(val.match(pattern)) {
                old = val;
                return true;
            } else {
                ele.value = old;
                return false;
            }
        }

html Code
< input type= "text" size= "20" onkeyup= "check (this)" >

how does this rule retain two decimal places

Mar.09,2021

add {0jue 2} to the original.

/^(\+|\-)?((([1-9]([0-9]*))|0)(\.[0-9]{0,2})?)?$/
Menu