Autoformat SSN while entering the number

javascript ssn mask
jquery validate social security number
format phone number javascript
js auto format phone number
jquery auto hyphen phone number
css to format ssn
javascript add dashes to ssn
ssn regular expression examples

I have a textfield and the user enters the SSN number. While entering itself it should format. Like On the change of the textField... it should format 999-999-999 in this way on the display itself.

<input id="ssn"/>

<script type="text/javascript">
    $('#ssn').keyup(function() {
        var val = this.value.replace(/\D/g, '');
        val = val.replace(/^(\d{3})/, '$1-');
        val = val.replace(/-(\d{2})/, '-$1-');
        val = val.replace(/(\d)-(\d{4}).*/, '$1-$2');
        this.value = val;
    });
</script>

Auto-format SSN when entering a number, <input id="ssn"/> <script type="text/javascript"> $('#ssn').keyup(function() { var val = this.value.replace(/\D/g, ''); val = val.replace(/^(\d{3})/, '$1-'); val  While technically it’s not legally required that the broker records your SSN if you refuse to provide it, the broker must make a good faith effort to collect it. As a result, and given the automation in the account opening process today, it would be virtually impossible for you to complete the account setup process without providing this.

The script from @kottenator was almost there, but it breaks the value every 3 digits, instead of 3, then 2, like the 000-00-0000 needed for Social Security numbers.

I did a little editing and modified it to work as intended. Hope this helps.

    <script type="text/javascript">
       $('#ssn1').keyup(function() {
          var val = this.value.replace(/\D/g, '');
          var newVal = '';
          if(val.length > 4) {
             this.value = val;
          }
          if((val.length > 3) && (val.length < 6)) {
             newVal += val.substr(0, 3) + '-';
             val = val.substr(3);
          }
          if (val.length > 5) {
             newVal += val.substr(0, 3) + '-';
             newVal += val.substr(3, 2) + '-';
             val = val.substr(5);
           }
           newVal += val;
           this.value = newVal.substring(0, 11);
        });
    </script>

ssn validate & format - JSFiddle, 2. // trap keypress - only allow numbers. 3. $('input.ssn').on('keypress', function(​event){. 4. // trap keypress. 5. var character = String.fromCharCode(event.which);. According to the U.S. Department of Justice, identity theft struck at least one person over the age of 12 in 7 percent of households in 2010. Identity theft doesn't happen solely by adding your Social Security number to an online application, but it's certainly possible unless you take steps to mitigate the risk.

@Dennis's answer was the best here, however it used JQuery to do the selector and the OP did not have a JQuery tag on this post, just JavaScript. Here is the VanillaJS version of the solution (or at least one way to do it :)

document.getElementById("ssn").onkeyup = function() {
  var val = this.value.replace(/\D/g, '');
  var newVal = '';

  if(val.length > 4) {
    this.value = val;
  }

  if((val.length > 3) && (val.length < 6)) {
    newVal += val.substr(0, 3) + '-';
    val = val.substr(3);
  }

  if (val.length > 5) {
    newVal += val.substr(0, 3) + '-';
    newVal += val.substr(3, 2) + '-';
    val = val.substr(5);
  }

  newVal += val;
  this.value = newVal;
};

Format data input, like social security number or phone number , Another exmaple, as we want a social security number. When the client types in 123456789, it should automatically populate in the field as  Allowing users to enter their phone number in any format forces cognitive work on them. Instead, take the load off users by auto-formatting their input for them. Making this change to your phone number field can prevent form abandonment and increase your conversion rate. Follow the best practice on phone number fields and stop making your users

I modified Dennis' script a bit removing the jquery elements. Not sure why the first two if clauses were added, so I removed them from this function. This function is a listener for the Titanium SDK using underscore.js. But you should be able to modify it to work with other JS API's.

$.usernameField.addEventListener('blur', function(param) {

    var inputString = param.value;

    if (inputString.length === 9 && _.isNumber(parseInt(inputString, 10))) {

        var val = inputString.replace(/\D/g, '');

        var outputString = '';

        outputString += val.substr(0, 3) + '-';
        outputString += val.substr(3, 2) + '-';
        val = val.substr(5);
        outputString += val;

        $.usernameField.value = outputString;

    }
});

Format SSN Input Box - Javascript, If the user enters as all numbers it shoud format as xxx-xx-xxxx. function fixSSN​(str) { var str = new String(str); str = str.replace(/[A-Za-z$-()\.]/  The thousand separator will automatically be added. 2. How to Format a License Number. Another type of input where it makes sense to apply this approach is the “License Number” input. The Envato Market license number or purchase code, for example, is 36 characters long and comprises letters, numbers, and a few dashes. Entering it correctly

$('#ssn').keyup(function() {
    var val = this.value.replace(/\D/g, '');
    var newVal = '';
    var sizes = [3, 2, 4];

    for (var i in sizes) {
      if (val.length > sizes[i]) {
        newVal += val.substr(0, sizes[i]) + '-';
        val = val.substr(sizes[i]);
      }
      else
        break;        
    }

    newVal += val;
    this.value = newVal;
});

jackocnr/intl-tel-input: A JavaScript plugin for entering and , A JavaScript plugin for entering and validating international telephone numbers Handle phone number extensions; The user types their national number and the When you initialise the plugin, the first argument is the input element, and the for the reasons listed here: #346 Disable and remove autoFormat feature. Part of Word 2016’s AutoCorrect function is a feature named AutoFormat. Whereas AutoCorrect fixes typos and common spelling boo-boos, AutoFormat fixes formatting fumbles. Enjoying automagical text in Word 2016 AutoFormat controls some minor text formatting as you type. All its settings are visible in the AutoCorrect dialog box’s AutoFormat as You Type tab, as shown …

Learning Mobile App Development: A Hands-on Guide to Building Apps , This code gets the value entered into the editAddress EditText, converts it to a string, and method is to set the phone number EditTexts to autoformat the add initTextChangedEvents(); to the onCreate method to set up the listeners when the  A few options exist for controlling some of ­Excel's AutoFormat behavior from the File tab by selecting Options, Proofing, AutoCorrect options, AutoFormat As You Type (as pictured below). However, these adjustment options do not completely disable Excel's automatic application of number formats as data are entered.

Form Design: How to Automatically Format User Input, This approach can greatly facilitate users when entering form data. Deploy parseInt() function to make sure the value is an integer (a round  Note: For REAL ID document purposes, you may not need a Social Security number (SSN) card as proof of an SSN. Instead, a recent pay stub, W-2 (wage and tax statement), SSA-1099 (Social Security Benefit Statement) or other tax documents showing your full name and entire SSN may be acceptable proof of your SSN.

Auto Format Phone Number with SINGLE JavaScript sentence , How to use cleave.js to auto format phone number based on user's country automatically on Duration: 3:46 Posted: 3 Dec 2018 Since 1973, social security numbers have been issued by our central office. The first three (3) digits of a person's social security number are determined by the ZIP Code of the mailing address shown on the application for a social security number. Prior to 1973, social security numbers were assigned by our field offices.

Comments
  • Actually the social security number are in format of 000-00-0000.
  • I've updated my solution to match NNN-NN-NNNN pattern.
  • This is almost there, but from what I've tried, it has two problems. First, whenever you hit a key in the middle of the ssn, the cursor goes to the end of the text. Second, unless you hold the 'delete' key down, when you click 'delete' on a dash it reappears right away. I've fixed the second one with a solution I'll show below, but I've hit a wall with the first one.
  • I like how it also limits the number of characters that you can put in
  • I've just read this solution, 2 years later :) Yes, my code formats the output as \d{3}-\d{3}-\d{3} but that's how it was requested in the question. For some reason I've forgot how many chars does SSN have. Thanks, @Dennis O'Neil / @Jason Hansen.
  • The OP only listed JavaScript in their tag. You should try to not mix in other API's in your answer since they might not know anything about Titanium SDK and underscore.js