简体   繁体   中英

how to mask first Five digit number on keyup using *

For eg while i am typing my ssn number my first 5 dight must be masked to *

  123456789    =>  *****6789

Note : on keyup it should check no of digits and mask based on it.

I came through this below example. It mask the entire nine digit.

https://codepen.io/anon/pen/VROrdo

I modified your code to mask first 5 characters. Also this is bullet proof if someone paste a copied number.

https://codepen.io/anon/pen/PLvRWw

// Replace first 5 numbers with astericks
if (displayVal.length < 6){
  displayVal = displayVal.replace(/[0-9]/g, '*'); 
}
else{
  displayVal = '*'.repeat(5) + val.slice(5);
}

使用以下命令更改displayVal replace方法regexr。

displayVal = displayVal.replace(/[0-9]+5/g,i=>"*****".slice(0,i.length));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM