Input box keyboard events, listeners can only enter integer and two decimals method

1. Enter integer

keyUpVal: function (e) {
            var input = $(e.currentTarget).get(0);
            input.value = input.value.replace (/ [^ \ D] / G, ''); // clear the character other than "digital" and "." 
            input.value = input.value.replace ( '',. '$ # $') Replace. (/\./ G, '') .replace ( '$ # $', '.' );
             IF (input.value.indexOf ( '.') <0 && input.value ! = '') { // above has been filtered, the control here that if no decimal point, can not be similar to the first 01,02 
                input.value = parseFloat (input.value);
            }
            if (input.value == '0') {
                input.value = '';
            }
        }

2. Enter up to two decimal places

keyUpVal1: function (e) {
            var input = $(e.currentTarget).get(0);
            input.value = input.value.replace (/ [^ \ D.] / G, ''); // clear the "numbers" and the characters other than "." 
            input.value input.value.replace = (/ \. 2 {,} / G,) '.'; // keep only the first remove excess. 
            input.value input.value.replace = (, '$ # $') Replace (/\./ '.'. G, '') .replace ( '$ # $', ''. );
            input.value = input.value.replace (.. / ^ (\ -) * (\ + D) \ (\ D \ D) * $ /, '$ 2. $ $. 3. 1'); // only enter two decimals 
            IF (input.value.indexOf ( '.') <0 && input.value! = '') { // above has been filtered, the control here that if no decimal point, can not be similar to the first 01,02 
                input.value = parseFloat (input.value);
            }
            IF ( '.' input.value.indexOf () == 0 ) {
                 // decimal 0 is added in the first top 
                input.value = '0' + input.value;
            }
        }

 

Guess you like

Origin www.cnblogs.com/wpp281154/p/11890147.html