Skip to content

Instantly share code, notes, and snippets.

@jeffwesson
Created April 26, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffwesson/fcb846eccfb90971655d39738a735004 to your computer and use it in GitHub Desktop.
Save jeffwesson/fcb846eccfb90971655d39738a735004 to your computer and use it in GitHub Desktop.
ssn formatter
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="text" id="x" maxlength="11" pattern="\d{3}\-\d{2}\-\d{4}">
<script>
var div = document.getElementById('x');
var disallowed = new RegExp('\D', 'g');
div.addEventListener('input', function (e) {
var text = e.target.value;
var trimmed = text.replace(disallowed, '');
e.target.value = formatSsn(trimmed);
});
var formatSsn = function (str) {
if (str.length === 3 || str.length === 6) {
return str + '-';
} else {
return str;
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment