This is one example of how I format the SSN on a form:
First I get rid of any punctuation a user might have put in and trim the number
Then I parse it out into the 3 groups of numbers
Then I put it back together in the correct format
The last thing I do is check for length, if the length is incorrect it just leaves it the way it was entered
This code doesn't cover every possible user entry, but it has worked pretty consistently for our application.
BeginningSubString := @Trim(@ReplaceSubstring(SSN;@NewLine: " " : "," : "-";""

);
firstthree := @Left(BeginningSubString; 3);
middletwo := @Right(@Left(BeginningSubString; 5);2);
lastfour := @Right(BeginningSubString; 4);
code := firstthree + "-" + middletwo + "-" + lastfour;
FinalString := @If(SSN = ""; "" ;@Length(@Trim(BeginningSubString)) != 9; SSN ; code);
FinalString