12.19.2006

OnChange script to validate Social Security numbers

This is fairly well documented in the SDK and on the newsgroups, but since it is such a frequently used kind of script I thought I'd throw it up on the blog. Place this script in the onChange event of a field that you use to store Social Security numbers and the script will format it properly (NNN-NN-NNNN) if the user enters 9 numbers, and alert them if they enter an incorrect amount of numbers.

var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
switch (sTmp.length)
{
case 9:
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 2) + "-" + sTmp.substr(5, 4);
break;

default:
alert('SSN must contain 9 numbers.');
break;
}
}


Hope you find this helpful. If you do, give the ads a click! Thanks!

0 comments:

 
ICU MSCRM © 2004-2009