js:银行卡号四个隔开,并用星号代替,只显示后四位,怎么写啊
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js:银行卡号四个隔开,并用星号代替,只显示后四位,怎么写啊相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text" type="number" name="" id="card" onkeyup="ck(this)" onKeyDown="del(this)">
<br>
<input type="text" id="show">
</body>
<script type="text/javascript">
var card='';
function ck(e)
if(e.value.length>card.length)
card=card+e.value.slice(-1);
e.value=e.value.slice(0,-1)+"*";
l=e.value.length+1;
if(!(l%5))
e.value=e.value+" ";
card=card+" ";
document.getElementById('show').value=card;
function del (e)
if(event.code=="Backspace")
if(e.value.slice(-1)==" ")
e.value=e.value.slice(0,-1);
card=card.slice(0,-1);
card=card.slice(0,-1);
</script>
</html>
输的太快会出问题
参考技术A 将字符串分割成数组,循环隐藏部分号码为星号,过滤银行卡号手机号
效果展示:
直接上代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> input{ width:800px; } </style> </head> <body> <input id="card" type="text" placeholder="请输入银行卡号"> <input id="phone" type="text" placeholder="请输入银行卡号"> <script> var cardInput = document.getElementById(\'card\'); var phoneInput = document.getElementById(\'phone\'); //模拟数据 var str = \'6228480402564890018\'; var phone = \'13367678989\'; //银行卡号过滤 function stringHidePart(strObj){ var strLength = strObj.length; var star = \'\'; var strRel = \'\'; if(strLength>6){ var hideSec = strObj.substring(3); //星号部分 for(var i=7;i<hideSec.length;i++){ star+= "*"; } }; strRel = strObj.substring(0,6) + star + strObj.substr(strObj.length-4); // strRel=strRel.replace(/\\s/g,\'\').replace(/(.{4})/g,"$1 "); return strRel; }; //银行卡号过滤 function stringHidePart2(strObj){ var strLength = strObj.length; var star = \'\'; var strRel = \'\'; if(strLength>3){ var hideSec = strObj.substring(3); //星号部分 for(var i=4;i<hideSec.length;i++){ star+= "*"; } }; strRel = strObj.substring(0,3) + star + strObj.substr(strObj.length-4); // strRel=strRel.replace(/\\s/g,\'\').replace(/(.{4})/g,"$1 "); return strRel; }; window.onload = function(){ // 判断后台数据是否存在 if(str.length>0){ cardInput.value = stringHidePart(str); cardInput.setAttribute(\'disabled\',\'disabled\') }else{ //用户输入 } if(phone.length>0){ phoneInput.value= stringHidePart2(phone); phoneInput.setAttribute(\'disabled\',\'disabled\') }else{ //用户输入 } } </script> </body> </html>
以上是关于js:银行卡号四个隔开,并用星号代替,只显示后四位,怎么写啊的主要内容,如果未能解决你的问题,请参考以下文章