input MM/YY

<input maxlength="5" class="js_card_pay" type="text" placeholder="MM/YY" onkeyup="value=value.replace(/[^\d]/g,'')" value="" data-flag="1">
                <script>
                    function insertStr(source, start, newStr) {
                        return source.slice(0, start) + newStr + source.slice(start)
                    }
                    $('.js_card_pay').on('keypress', function() {
                        var flag = $(this).data().flag;
                        if(flag == 1) {
                            $(this).data('flag', '2');
                        } else {
                            return false;
                        }
                    })
                    $('.js_card_pay').on('keyup', function() {
                        $(this).data('flag', '1');
                        var currentVal = $(this).val();
                        if(currentVal.length == 1){
                            if (currentVal > 1) {
                                $(this).val("");
                            }
                        }else if(currentVal.length == 2){
                            if (currentVal == 00) {
                                currentVal = currentVal.substring(0, currentVal.length - 1);
                                $(this).val(currentVal);
                            }else if (currentVal > 12) {
                                currentVal = currentVal.substring(0, currentVal.length - 1);
                                $(this).val(currentVal);
                            }
                        }else if(currentVal.length > 2){
                            $(this).val(insertStr(currentVal, 2, '/'));
                        }
                    })
                </script>

你可能感兴趣的:(前端,javascript)