Debanjan Banerjee
Debanjan Banerjee

Reputation: 73

how to replace inital digits of ssn with '*' while using mask of jquery

how to replace inital digits of ssn with '*' while using mask of jquery

for example: 123-45-6789 will be inserted as ***-**-6789 ,but when we fetch the value we should get 123456789

Upvotes: 4

Views: 13739

Answers (2)

mVChr
mVChr

Reputation: 50195

Given HTML as follows:

<form id="ssn_form">
    <input name="ssn" id="ssn" type="text">
    <button id="submit">Submit</button>
</form>​

You can store the SSN in a data attribute on blur and mask the value. Then on form submit you can repopulate the value into the input right before submitting the form:

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}/, '*****'));
    };

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

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

$('#ssn_form').submit(function(ev){
    ev.preventDefault();
    retrieveValue.call($('#ssn')[0], ev);
    $(this).submit();
});

See demo

Upvotes: 3

swapnesh
swapnesh

Reputation: 26732

Without any code supply i think this is the best you can try with :)

INPUT - 123-45-6789

OUTPUT- ***-**-6789

Check this --

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
function mask(f){
tel='';
var val =f.value.split('');
for(var i=0;i<val.length;i++){
if(i<3){val[i]='*'}
if(i<6 && i>3 ){val[i]='*'}
tel=tel+val[i]
}
f.value=tel;
}
</script>
</head>
<body>
<form>
<input name="phone" type="text" onblur="mask(this)">
</form>
</body>
</html> 

Upvotes: 0

Related Questions