John Cooper John Cooper - 2 years ago 126
Javascript Question

Autoformat SSN while entering the number

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

in this way on the display itself.

Answer Source
<input id="ssn"/>

<script type="text/javascript">
    $('#ssn').keyup(function() {
        var val = this.value.replace(/\D/g, '');
        var newVal = '';
        while (val.length > 3) {
          newVal += val.substr(0, 3) + '-';
          val = val.substr(3);
        newVal += val;
        this.value = newVal;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download