[Solved] Backspace not working inside KeyUp event of TextBox in jQuery

Last Reply one month ago By pandeyism

Posted one month ago

hi 

I have used below code for date format and it went well. but when im using backspace it is not clearing  "-" this dash. how to make it work 

$("#masterDataFeedDate_Id").on('input paste keyup', function() {
    if ($(this).val().length === 4){
        $(this).val($(this).val() + "-");
    }
    else if ($(this).val().length === 7){
        $(this).val($(this).val() + "-");
    }
});

 

Posted one month ago

Hey gouthamgou,

Please refer below sample.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#masterDataFeedDate_Id").on("keyup", function (e) {
            if (e.which == 8) {
                return true;
                e.preventDefault();
            }
            if ($(this).val().length === 4) {
                $(this).val($(this).val() + "-");

            }
            else if ($(this).val().length === 7) {
                $(this).val($(this).val() + "-");
            }
        });
    });
</script>
<div>
    Date :
    <input  type="text" id="masterDataFeedDate_Id" />
</div>

Demo