Question

I have a script that hides everything but the last four numbers of a social security number. I need to adapt it to allow dashes and change the math to show the last four of an 11 digit string, rather than a 9 digit string. Here's the line where the character allowances are:

$this.val($this.val().replace(/^\d{5}/, '*****'));

And here's the jsfidle: http://jsfiddle.net/d5KaJ/45/

And here's the script:

var retrieveValue = function(ev){
        var $this = $(this),
            val = $this.data('value');

        if (val) {
            $this.val(val);
        }
    },
    hideValue = function(ev){
        var $this = $(this);

        $this.data('value', $this.val());
        $this.val($this.val().replace(/^\d{5}/, '*****'));
    };

$('#field_a7afui').focus(retrieveValue);

$('#field_a7afui').blur(hideValue);

$('#form_hv3hcs').submit(function(ev){
    ev.preventDefault();
    retrieveValue.call($('#field_a7afui')[0], ev);
    alert($('#field_a7afui').val());
    hideValue.call($('#field_a7afui')[0], ev);
});

Again, I just need it to allow numbers and dashes and I need it to show the last four digits of an 11-digit soc sec number, e.g., XXX-XX-XXXX

Thanks in advance for any help!

Was it helpful?

Solution

Try

hideValue = function(ev){
    var $this = $(this);

    $this.data('value', $this.val());
    $this.val(function(idx, val){
        var mask = val.match(/^(.*?)(\d{4})$/);

        return (mask[1] ? mask[1].replace(/\d/g, '*') : '') + (mask[2] ? mask[2] : '')
    });
};

Demo: Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top