I have a textbox and a button that serves as a mask/unmask button. It calls the checkSSN() function. By default, the text should be masked. But I need to have it in a certain format. SSN should be 9 alphanumeric characters. When it's masked, certain characters should be changed to 'X' (E.g. AF25QT34G to AXXXXT34G). The code below does the masking however I wanted it to do the masking while I'm still inputting. Also, how do I retrieve the original value to the code behind. Or is there any other way to make it convenient for me to do?
function checkSSN(){
if(ssn== undefined)
var ssn= "";
if(ssn == "")
ssn = document.getElementById('<%= txtSSN.ClientID %>').value;
var count = (document.getElementById('<%= txtSSN.ClientID %>').value.match(/X/g) || []).length;
if(count > 1)
showSSN(ssn);
else
hideSSN(ssn);
}
function hideSSN(ssn){
var maskchar = 'X';
var nonmaskdigit = 4;
var firstpart = ssn.substring(0,1);
var middlepart = maskchar.repeat(ssn.length - 5);
var lastpart = ssn.substring(ssn.length - nonmaskdigit);
var maskedSSN = firstpart + middlepart + lastpart;
document.getElementById('<%= txtSSN.ClientID %>').value = ssn;
}
function showSSN(ssn){
document.getElementById('<%= txtSSN.ClientID %>').value = ssn;
ssn = "";
}