Hi, I modified Guido's code to format the number depending on what was selected from an option set. Our ID field also allows a temporary number, so I removed the extra validations for the ID number.
function formatSSN(context, OptionSetParameter) {
var SSN = context.getEventSource();
var idtype = " ";
var option = Xrm.Page.getAttribute(OptionSetParameter).getSelectedOption();
//alert("OptionSet Test: " + option.text);
if (option != null)
{
idtype = option.text;
// alert("Value from Exeuction " + SSN.getValue() + " OptionSetParameter " + option.text);
// Verify that the field is valid
if (typeof (SSN) != "undefined" && SSN != null) {
if (SSN.getValue() != null && idtype != "Green Card") {
// Remove any special characters
var sTmp = SSN.getValue().replace(/[^0-9,A-Z,a-z]/g, "");
var isValid = validateSSN(sTmp);
if (isValid == false) {
SSN.setValue(null);
DisplayNotification("Invalid Tax ID. Please ensure length is 9 and re-enter ID .", "ERROR", "!");
}
else {
// format the translated number
switch (idtype) {
case "TIN":
SSN.setValue(sTmp.substr(0, 2) + "-" + sTmp.substr(2, 7));
break;
case "Social Security":
case "SSN":
SSN.setValue(sTmp.substr(0, 3) + "-" + sTmp.substr(3, 2) + "-" + sTmp.substr(5,4));
break;
default:
// Remove any special characters
sTmp = sTmp.replace(/[^0-9,A-Z,a-z]/g, "");
SSN.setValue(sTmp);
break;
}
}
}
}
}
else
{alert("Please select an ID type.")}
}
function validateSSN(sTmp) {
var area = parseInt(sTmp.substring(0, 3));
return (
sTmp.length === 9 &&
sTmp.match(/^[1-9][0-9]{2}[1-9][0-9]{1}[1-9][0-9]{3}/) );
}