Simple Form SSN Validation Script

Author: Tripp W Black

Created: 03/19/2001 at 02:35 PM

 

Category:
Notes Developer Tips
Forms/Subforms, JavaScript

TITLE: Simple SSN validation script
This tip was submitted by Steven Jacobs, an Internet specialist
in Tennessee.

This code strips unwanted dashes and validates filed entry.

CODE:
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 instructions" +
"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");
}
END CODE
=================================================

previous page