Derived from this post at StackOverflow
A script for formatting zipcodes (yes I know you probably don't need to format a 5 digit code of number!)
function OnZipFieldChange(context) { var value = context.getEventSource().getValue(); if (typeof(value) != "undefined" && value != null) { value = formatZipNumber(value); } context.getEventSource().setValue(value); } function formatZipNumber(inputValue) { var scrubbed = inputValue.toString().replace(/[^0-9]/g, ""); var fiveDigitFormat = /^\(?([0-9]{5})$/; var nineDigitFormat = /^\(?([0-9]{5})\)?[-. ]?([0-9]{4})$/; if (fiveDigitFormat.test(scrubbed)) { return scrubbed.replace(fiveDigitFormat, "$1"); } else if (nineDigitFormat.test(scrubbed)) { return scrubbed.replace(nineDigitFormat, "$1-$2"); } return inputValue; }
A script for formatting SSN's
function OnSSNFieldChange(context) { var value = context.getEventSource().getValue(); if (typeof(value) != "undefined" && value != null) { value = formatSSN(value); } context.getEventSource().setValue(value); } function formatSSN(inputValue) { var scrubbed = inputValue.toString().replace(/[^0-9]/g, ""); var ssn = /^\(?([0-9]{3})\)?[-. ]?([0-9]{2})[-. ]?([0-9]{4})$/; if (ssn.test(scrubbed)) { return scrubbed.replace(ssn, "$1-$2-$3"); } return inputValue; }
A script for formatting phone numbers
function OnPhoneFieldChange(context) { var value = context.getEventSource().getValue(); if (typeof(value) != "undefined" && value != null) { value = formatPhoneNumber(value); } context.getEventSource().setValue(value); } function formatPhoneNumber(inputValue) { var scrubbed = inputValue.toString().replace(/[^0-9]/g, ""); var sevenDigitFormat = /^\(?([0-9]{3})[-. ]?([0-9]{4})$/; var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; var extDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})?([0-9]*)$/; if (tenDigitFormat.test(scrubbed)) { return scrubbed.replace(tenDigitFormat, "($1)-$2-$3"); } else if (sevenDigitFormat.test(scrubbed)) { return scrubbed.replace(sevenDigitFormat, "$1-$2"); } else if (extDigitFormat.test(scrubbed)) { return scrubbed.replace(extDigitFormat, "($1)-$2-$3 x$4"); } return inputValue; }
No comments:
Post a Comment