自动截断银行卡号的文本框

 1 <!DOCTYPE html>
 2 <html lang="cn">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>自动截断银行卡号</title>
 6     <style type="text/css">
 7 
 8         #bankCard{
 9             width: 600px;
10             height: 50px;
11             line-height: 50px;
12             font-size: 30px;
13             font-weight: 700;
14             color: #f00;
15         }
16 
17     </style>
18 </head>
19 <body>
20     <h1>银行卡自动截断</h1>
21     <hr>
22     <h2>请输入银行卡号: </h2>
23 
24     <input type="text" id="bankCard" maxlength="23">
25 
26     <script>
27 
28         // 绑定事件
29         var input = document.getElementById('bankCard');
30         // 兼容性
31         if (document.all) {
32             input.onpropertychange = ck;
33         } else {
34             input.oninput = ck;
35         }
36 
37         // 截断函数
38         function ck() {
39             // 只能输入数字
40             // 限制输入范围
41             // input.value = input.value.replace(/\D/g, '').replace(/(\d{4})/g, '$1 ').replace(/ $/g, '');
42             input.value = input.value.replace(/\D/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
43         }
44 
45     </script>
46 </body>
47 </html>
这是一个关于自动截断银行卡号的Js效果 就是输入四位数字后 自动截断 有不足之处希望大神指点一下 !!!

 

posted @ 2018-09-05 19:27  GodH  阅读(322)  评论(0编辑  收藏  举报