JSFiddle - React, Tailwind, and code Playground
by st3fan
HTML
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<input type="text" value="19940522-1234" />
JavaScript
$(function() {
$('input').blur(function(e) {
$(this).val( formaters.ssn( $(this).val() ) );
});
});
var formaters = {
ssn: function(value) {
value = value.replace(/\D/g, '');
if (value.length === 12)
value = value.substring(2);
return value;
}
};
// Test functions
var assert = function(name, result) {
var test = arguments.length === 2 ? result :
_.isEqual(result, arguments[2]);
test ? console.info('PASSED', name) : console.warn('FAILED', name);
};
// Unit tests
assert('Format SSN, A', formaters.ssn('8604281234'), '8604281234');
assert('Format SSN, B', formaters.ssn('19860428-1234'), '8604281234');
assert('Format SSN, C', formaters.ssn('awd 01 da23 %/() 45_6,7 8 '), '012345678');
assert('Format SSN, D', formaters.ssn('01234567890123456789'), '01234567890123456789');
assert('Format SSN, E', formaters.ssn('012345678901'), '2345678901');