Mask last 4 digits

Mask last 4 digits

I am trying to hide the last 4 digits of an account number.  The account number lengths vary.  The code I am trying to develop is not working correctly.  Does anyone have any suggestions?  Thanks

Code is below:

$('input:text[name=accountNumber]').change(function(e) {
        var lastFourDigit = "";
        var mask = "";
        var oldlength = "" ;
        var newlength = "";
        if($('#accountNumber')){
            $('#hdnAccountNumber').val($('#accountNumber').val());
            if($('#accountNumber').val().length > 13 ){
                lastFourDigit =  $('#accountNumber').val().substr(13,$('#accountNumber').val().length);
                oldlength = $('#accountNumber').val().length;
                newlength = lastFourDigit - 4;

                for (i = 0; i < 13; i++) mask += "*";
                     mask = mask+ newlength;
                    $('#accountNumber').val(mask);
            } else{
                     for (i = 0; i < $('#accountNumber').val().length; i++) mask += "*"
            }      $('#accountNumber').val(mask);
        }
    });