Tip

Simple SSN Validation Script

Simple SSN Validation Script. Strips unwanted dashes and validates filed entry.
function checkSSNLength()
{
var q = document.forms[0].SocSecNo.value;
var a = q.charAt(0);
var b = q.charAt(1);
var c = q.charAt(2);
var d = q.charAt(3);
var e = q.charAt(4);
var z = q.charAt(5);
var y = q.charAt(6);
var h = q.charAt(7);
var i = q.charAt(8);
var j = q.charAt(9);
var k = q.charAt(10);
if ((q.length == 11 ) && (d == "-") && (y == "-"))
{
var a = q.charAt(0);
var b = q.charAt(1);
var c = q.charAt(2);
var e = q.charAt(4);
var z = q.charAt(5);
var h = q.charAt(7);
var i = q.charAt(8);
var j = q.charAt(9);
var k = q.charAt(10);   
var finalString = a + b + c + e + z + h + i + j + k;
document.forms[0].SocSecNo.value = finalString;  
checkSSINumber();            
}else
if (q.length == 9)
{
checkSSINumber();  
return true;    
}else
if ((q.length > 11) || (q.length < 9))
{
alert("As we all know, unless the US Government has changed Social Security Policyn" +
"overnight, the maximum number of digits for a social security number is 11 if you include dashes.nnn" +
"For example: 123-45-6789 or 123456789.  If you need additional help or instructionsn" +
"pertaining to this matter please contact your local representative to discuss this matter....Thank You!!!");
document.forms[0].SocSecNo.focus();
document.forms[0].SocSecNo.select();
return false;
}
} 
function checkSSINumber()
{
var f = document.forms[0].SocSecNo.value;
var tempNum = Math.floor(f/10000);
var tempNum1 = Math.floor(tempNum/1000);
var tempNum2 = Math.floor(tempNum1/100);
var tempNum3 = Math.floor(tempNum2/10);

if ((f.length < 9) || (f.length > 9) || (tempNum3 != 0))
{
alert("Not a valid SSN number");
document.forms[0].SocSecNo.focus();
document.forms[0].SocSecNo.select();
}
else
alert("Valid SSN");
}

This was first published in February 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.