Skip to main content
Participant
July 22, 2016
Question

SSN JAvascript

  • July 22, 2016
  • 1 reply
  • 18814 views

Hi all. I need help with the javascript code for Social security number in the xxx-xx-xxxx format. Any help will be greatly appreciated.

    This topic has been closed for replies.

    1 reply

    David_Powers
    Inspiring
    July 22, 2016

    What do you mean by "JavaScript code"? Are you looking for a regular expression to match that pattern? If so =

    var ssnPattern = /^\d{3}-\d{2}-\d{4}$/;

    That assumes that the SSN is the only value being tested. In other words, it's not buried in a longer string.

    rinzmannAuthor
    Participant
    July 22, 2016

    Dave I need it in a form field I am creating in a pdf, so that when someone fills that portion of the pdf, the numbers will appear in the format I stated above without them keying in the dash.

    David_Powers
    Inspiring
    July 22, 2016

    Try this:

    <body>

    <form id="form1" name="form1" method="post">

     

      <label for="ssn">Social Security Number:</label>

      <input type="text" name="ssn" id="ssn">

     

      <input type="submit" name="submit" id="submit" value="Submit">

    </form>

    <script>

      var ssn = document.getElementById('ssn');

      ssn.addEventListener('keyup', function() {

      if (!this.value.match(/^\d+$/)) {

      alert('Please use numbers only');

      }

      }, false);

      ssn.addEventListener('blur', function() {

      if (!this.value.match(/^\d{9}$/)) {

      alert('SSN should contain 9 digits');

      } else {

      var updated = this.value.replace(/^(\d{3})(\d{2})(\d{4})$/, '$1-$2-$3');

      this.value = updated;

      }

      })

    </script>

    </body>