Others



Auto Format Credit card Number using Javascript [16 Digit Number, Add Sapace Every 4 Digits]


The following example shows, how to auto format credit card number using JavaScript.

Demo

Example
<html>
  <body>
    <input type='text' onkeypress='return formats(this,event)' onkeyup="return numberValidation(event)" placeholder='Enter Credit Card No' >
    <script>
      function formats(ele,e){
        if(ele.value.length<19){
          ele.value= ele.value.replace(/\W/gi, '').replace(/(.{4})/g, '$1 ');
          return true;
        }else{
          return false;
        }
      }
      
      function numberValidation(e){
        e.target.value = e.target.value.replace(/[^\d ]/g,'');
        return false;
      }
    </script>
  </body>
</html>
Try it Yourself