1、原生js写法
function () { document.getElementById('bankCard').onkeyup = function (event) { var v = this.value; if(/\S{5}/.test(v)){ this.value = v.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 "); } }; }(); |
2、jQuery写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="text" id="J_BankCard"/> <script src="http://res.wdphp.com/jquery/1.10.2/jquery.min.js"></script> <script> !function () { $('#J_BankCard').on('keyup mouseout input',function(){ var $this = $(this), v = $this.val(); /\S{5}/.test(v) && $this.val(v.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g, "$1 ")); }); }(); </script> </body> </html> |
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!