Skip to content

Commit b356981

Browse files
authored
Add files via upload (ServiceNowDevProgram#711)
1 parent 76ad8c3 commit b356981

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Regex To match ssn formats XXXXXXXXX or XXX-XX-XXXX
2+
var ssn = /^(?:\d{3}-\d{2}-\d{4}|\d{9})$/;
3+
//Example Input
4+
var input = '123-12-1234';
5+
//Boolean variable to dictate if you want hyphens or not
6+
var includeHyphen = true;
7+
//Return value to avoid multiple different returns
8+
var retval = '';
9+
//If it matches the format of XXXXXXXXX or XXX-XX-XXXX
10+
if(ssn.test(input)){
11+
//If the result will have hyphens and input already includes them
12+
if (includeHyphen && input.includes('-')){
13+
//Hyphens are already in place
14+
retval = input;
15+
}
16+
//Else if the result will have hyphens and the input did not include them
17+
else if (includeHyphen && !(input.includes('-'))){
18+
//Adds hyphens in the format of an SSN i.e. XXX-XX-XXXX
19+
retval = input.replace(/(\d{3})(\d{2})(\d{4})/, "$1-$2-$3");
20+
}
21+
//The returned value will not have hyphens included
22+
else if (!(includeHyphen)){
23+
//Removes all hyphens in the input
24+
reval = input.replace('-','');
25+
}
26+
}
27+
//Else the input is not in the form of a legal ssn
28+
else{
29+
console.log("'"+ input + "' is not a legal 9-digit SSN");
30+
}
31+
32+
return retval;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This script will both check if a given string is an SSN (9 numerical characters with out hyphens or 12 with hyphens in the 4th and 7th positions) and then return a formatted version of the string as per your choice including or excluding hyphens via the "includeHyphens" variable. If used is servicenow this could be instead mapped to a field rather than set in the script but allows for a more programmically approach to the solution. If the input does not match the form of an SSN (see above) it will console log out that it's not a valid SSN. This includes cases where only one of the 2 hyphens are present (i.e. XXX-XXXXXX or XXXXX-XXXX). This could be expanded to check if the input is in one of those two forms and correct for it without failing but I've chosen to leave the script in this form.

0 commit comments

Comments
 (0)