MkSSN Function:

The MkSSN function formats a string or long to look like a US Social Security Number. The resulting output of MkSSN is a string regardless of the type of input passed to MkSSN. If the information passed to MkSSN cannot be recognized as a Social Security Number, MkSSN returns a Null value. Before attempting to recognize an expression as a Soc. Sec. Number, MkSSN will remove any non numeric characters from the input. Only the resulting numeric characters are checked and formatted. The output of MkSSN is always eleven characters: xxx-xx-xxxx unless the input is invalid, in which case MkSSN returns Null. The other required argument is addspacers. It is a boolean indicating whether or not to add a non-breaking HTML space between each character in a valid social security number (for display purposes only).

Syntax:
string = MkSSN(socialsecuritynumber, addspacers)
Example Usage:
<%
Response.Write MkSSN(123456789, True)
 ' MkSSN returns 1 2 3 - 4 5 - 6 7 8 9

Response.Write MkSSN("123456789abcdefg", False)
 ' MkSSN returns 123-45-6789

Response.Write MkSSN("1gn2cbgd3gn45gdn67gn89", True)
 ' MkSSN returns 1 2 3 - 4 5 - 6 7 8 9

Response.Write MkSSN(12345678910, True)
 ' MkSSN returns Null (too many numeric characters)

Response.Write MkSSN("abcdefghi", False)
 ' MkSSN returns Null
%>
ASP Source Code:
<%
Private Function MkSSN(byVal SocialSecurityNumber, _
    byVal BoolAddSpacers)
	dim strIn, i, tmp, lngCt, strOut
	strIn = SocialSecurityNumber
	i = 1
	do
		tmp = Mid( strIn, i, 1 )
		If Not IsNumeric( tmp ) then
			strIn = Trim( Replace( _
			    strIn, tmp, "" ) )
		Else
			i = i + 1
		End If
	loop until i > Len( strIn )
	strIn = Trim( strIn )
	lngCt = Len( strIn )
	if lngCt <> 9 then
		MkSSN = Null
		Exit Function
	end if	
	strIn = CStr( strIn )
	strIn = CStr( Left( strIn, 3 ) & "-" & _
		Mid( strIn, 4, 2 ) & "-" & _
		Right( strIn, 4 ) )
	lngCt = Len( strIn )
	if lngCt <> 11 then
		MkSSN = Null
		Exit Function
	end if	
	If BoolAddSpacers Then
		for i = 1 to len( strIn )
			tmp = Mid(strIn, i, 1)
			strOut = strOut & tmp & " "
		next
	Else
		strOut = strOut & strIn
	End If
	MkSSN = strOut
End Function
%>