SSN Validation Script - Servicenow

Allowed Social Security number format: XXX-XX-XXXX


function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearMessages();
// Regular expression for validating SSN (Allowed Format: XXX-XX-XXXX)
var ssnPattern = /^\d{3}-\d{2}-\d{4}$/;
var validateSSN = ssnPattern.test(newValue);
//console.log(validateSSN + '-----validateSSN SSN');
if (!validateSSN) {
var errorMessage = 'SSN "'+newValue+'" is incorrect. Use the format XXX-XX-XXXX';
g_form.addErrorMessage(errorMessage);
g_form.showFieldMsg('ssn', errorMessage, 'error');
g_form.clearValue('ssn');
}

}