银行账号每4位插入一个空格

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><title>银行卡号4位空格</title><script src="http://j2.58cdn.com.cn/js/jquery-1.8.3.js"></script></head><body><input type="text" id="kahao" /><script>$(function() {$('#kahao').on('keyup', function(e) {//只对输入数字时进行处理if((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105 )){//获取当前光标的位置var caret = this.selectionStart//获取当前的valuevar value = this.value//从左边沿到坐标之间的空格数var sp = (value.slice(0, caret).match(/\s/g) || []).length//去掉所有空格var nospace = value.replace(/\s/g, '') //重新插入空格var curVal = this.value = nospace.replace(/(\d{4})/g, "$1 ").trim() //从左边沿到原坐标之间的空格数var curSp = (curVal.slice(0, caret).match(/\s/g) || []).length//修正光标位置this.selectionEnd = this.selectionStart = caret + curSp - sp }}) }) </script></body></html>

转载于:https://www.cnblogs.com/nixiaodong/p/6912587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值