Masking last 4 digits in JavaScript

后端 未结 7 1698
遥遥无期
遥遥无期 2020-12-09 05:39

Here I am using regex pattern to mask last 4 digits for Credit Card number.

$(\"#ccnumber\").html(ccnbr); //ccnumber refers to div ID

$(\"#ccnumber\").text(         


        
7条回答
  •  孤街浪徒
    2020-12-09 06:07

    This is a pure javascript answer.

    function cardHide(input) {
      //use slice to remove first 12 elements
      let first12 = input.slice(0, 12);
      //define what char to use to replace numbers
      let char = '*'
      let repeatedChar = char.repeat(input.length -4);
      // replace numbers with repeated char
      first12 = first12.replace(first12, repeatedChar);
      //concat hidden char with last 4 digits of input
      let hiddenNumbers = first12 + input.slice(input.length-4);
      
    
      //return
      return hiddenNumbers;
    }
    

提交回复
热议问题