arlyxiao / best-practice

1 stars 0 forks source link

Format ssn #9

Open arlyxiao opened 3 years ago

arlyxiao commented 3 years ago

https://stackoverflow.com/questions/7685175/autoformat-ssn-while-entering-the-number/7685345

$('#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);
        });
arlyxiao commented 2 years ago
function formatNumber(num) {
  return num.toString().replace(/\D/g,'').replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

 function recoverNumber(num) {
    return num.toString().replace(/\D/g,'').replace(/,/g, '');
  }