window.onload =function() {
document.getElementById("input_num").oninput =function() {
this.value =this.value.replace(/\s/g,'').replace(/\D/g,'').replace(/(\d{})(?=\d)/g,"$1 ");;
};
};

方案一: 缺点,光标不能定位

handleKeyUp = () => {
const target = document.getElementsByClassName('cardNumber')[].firstChild
target.onkeyup = e => {
// 只对输入数字时进行处理
if ((e.which >= && e.which <= ) ||
(e.which >= && e.which <= )) {
// 获取当前光标的位置
const caret = target.selectionStart
// 获取当前的value
const value = target.value
// 从左边沿到坐标之间的空格数
const sp = (value.slice(, caret).match(/\s/g) || []).length
// 去掉所有空格
const nospace = value.replace(/\s/g, '')
// 重新插入空格
const curVal = target.value = nospace.replace(/(\d{})/g, '$1 ').trim()
// 从左边沿到原坐标之间的空格数
const curSp = (curVal.slice(, caret).match(/\s/g) || []).length
// 修正光标位置
target.selectionEnd = target.selectionStart = caret + curSp - sp
}
}
}

方案二: 缺点(某些浏览器不工作,部分浏览器删除时光标跳动,如支付宝默认浏览器)

let isDelete = false; // globle param
// getCursortPosition
const getCursortPosition = (textDom) => {
let cursorPos =
if (document.selection) { // ie 10 9 8 7 6 5
// IE Support
textDom.focus()
let selectRange = document.selection.createRange()
selectRange.moveStart('character', -textDom.value.length)
cursorPos = selectRange.text.length
} else if (textDom.setSelectionRange) { // ie 11 10 9 ff safiri chrome
// webkit support
textDom.focus()
cursorPos = textDom.selectionStart
}
return cursorPos
} // setCursorPosition
const setCursorPosition = (textDom, pos) => {
if (textDom.setSelectionRange) {
textDom.focus()
textDom.setSelectionRange(pos, pos)
} else if (textDom.createTextRange) {
// IE Support
const range = textDom.createTextRange()
range.collapse(true)
range.moveEnd('character', pos)
range.moveStart('character', pos)
range.select()
}
} export const handleKeyUp = target => {
target.onkeyup = () => {
const elem = target
//for some andriod system keyboard can not input (eg: vivo)
setTimeout(() => {
const str = elem.value
let currentPos = getCursortPosition(elem)
let posAfterText = ''
let posPreText = ''
let isNextBlank = false//the next is blank or not
let isPreBlank = false
let isLastPos = true
if (currentPos != str.length) { // not the last one
posAfterText = str.substr(currentPos, )
posPreText = str.substr(currentPos - , )
isNextBlank = /^\s+$/.test(posAfterText)
isPreBlank = /^\s+$/.test(posPreText)
isLastPos = false
}
if (elem.value.length <= ) { // maxlength
elem.value = elem.value.replace(/\s/g, '').replace(/(\w{})(?=\w)/g, '$1 ')
}
if (isDelete) {
if (isPreBlank) {
setCursorPosition(elem, currentPos - )
} else {
setCursorPosition(elem, currentPos)
}
} else if (!isLastPos) {
if (isNextBlank) {
setCursorPosition(elem, currentPos + )
} else {
setCursorPosition(elem, currentPos)
}
} else {
setCursorPosition(elem, elem.value.length)
}
}, )
}
} export const handleKeyDown = target => {
target.onkeydown = () => {
isDelete = window.event.keyCode == // the flag of delete
}
}

方案三,比较完美的解决方案,支持各种终端浏览器输入, 任意位置删除输入且光标不会跳动。

export const getCursorPosition = textDom => {
let cursorPos =
if (textDom.setSelectionRange) {
// webkit support
textDom.focus()
cursorPos = textDom.selectionStart
}
return cursorPos
} export const setCursorPosition = (textDom, pos) => {
if (textDom.setSelectionRange) {
textDom.focus()
textDom.setSelectionRange(pos, pos)
}
} const handleKeyUp = e => {
const { target } = e
const elem = target
// for some andriod system keyboard can not input (eg: vivo)
setTimeout(() => {
const str = elem.value
const currentPos = getCursorPosition(elem)
let posAfterText = ''
let posPreText = ''
let isNextBlank = false// the next is blank or not
let isPreBlank = false
let isLastPos = true
if (currentPos !== str.length) { // not the last one
posAfterText = str.substr(currentPos, )
posPreText = str.substr(currentPos - , )
isNextBlank = /^\s+$/.test(posAfterText)
isPreBlank = /^\s+$/.test(posPreText)
isLastPos = false
}
if (elem.value.length <= ) { // maxlength
elem.value = elem.value.replace(/\s/g, '').replace(/(\w{})(?=\w)/g, '$1 ')
}
if (e.keyCode === ) { // delete key
if (isPreBlank) {
setCursorPosition(elem, currentPos - )
} else {
setCursorPosition(elem, currentPos)
}
} else if (!isLastPos) {
if (isNextBlank) {
setCursorPosition(elem, currentPos + )
} else {
setCursorPosition(elem, currentPos)
}
} else {
setCursorPosition(elem, elem.value.length)
}
}, )
}

方案三(优化方案, 不支持低版本ie, 支持低版本ie见方案三)

js实现卡号每四位空格分隔的更多相关文章

  1. js验证身份证号,超准确

    js验证身份证号,超准确 看程序前先来了解下身份证号的构成:身份证号分为两种,旧的为15位,新的为18位.身份证15位编码规则:dddddd yymmdd xx p    其中 dddddd:地区码  ...

  2. sqlserver卡号段分组

    之前给上海一家电子商务公司做一个卖卡系统,遇到了卡号段分组的问题.刚开始没什么好的实现方法,遂在博客园求助但未果,没法自己研究sql,终于搞定. 问题描述: 有个卡库存表,有个卡号字段,假设数据:16 ...

  3. C++中int转为char 以及int 转为string和string 转int和空格分隔字符串

    1.对于int 转为char 直接上代码: 正确做法: void toChar(int b) { char u; ]; _itoa( b, buffer, ); //正确解法一 u = buffer[ ...

  4. 金蝶核算项目余额表卡号余额与天财商龙CRM卡号余额对比

    金蝶核算项目余额表卡号余额与天财尚龙CRM卡号余额对比 由于历史遗留问题,财务一直不调账,修改核算科目卡号与天财商龙CRM系统一直,只能用VBA把卡号前缀修改成两边一致. 再通过,Power BI D ...

  5. highlight.js 设置行号

    原文地址:highlight.js 设置行号 博客地址:http://www.extlight.com 一.背景 笔者在开发这套博客系统时使用 Editormd 作为 Markdown 编辑器,由于不 ...

  6. Linux下读取RFID卡号(C串口编程)

    由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...

  7. .net第三方数据库物理卡号同步功能实现

    本地数据库用的是Oracle,第三方数据库是SQL Server,连接字符串保存在web.config里面. 第三方数据库为增量,每次读取要记录读取的最大位置.我是保存在本地txt文件里面. //保存 ...

  8. Xamarin如何使用终端设备的NFC功能传递卡号等信息给Web页面(Android)

    一.前提条件,App必须具有NFC权限. 二.项目中加入监控类NFCCatchActivity.cs [Activity(Label = "NFCCatch",Theme = &q ...

  9. vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)

    自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...

随机推荐

  1. electron 创建窗口2

    /** * 窗口管理类,单例,负责创建所有窗口,保存窗口实例 */ const path = require('path'); const os = require('os'); const EucW ...

  2. Spring 是如何解决并发访问的线程安全性问题的

    springmvc的controller是singleton的(非线程安全的),这也许就是他和struts2的区别吧!和Struts一样,Spring的Controller默认是Singleton的, ...

  3. 字符设备驱动笔记——poll机制分析(七)

    poll机制分析 所有的系统调用,基于都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的内核函数为:sys_open. ...

  4. Mybatis Spring multiple databases Java configuration

    https://stackoverflow.com/questions/18201075/mybatis-spring-multiple-databases-java-configuration ** ...

  5. python2 除法保留两位小数

  6. jquery全部版本

    http://www.jb51.net/zt/jquerydown.htm 这个地方实时更新jquery版本

  7. 获取IP和mac地址

    1.获取IP static string GetLocalIp() { string hostname = Dns.GetHostName();//得到本机名 //IPHostEntry localh ...

  8. 【WPF】给下拉列表ComboBox绑定数据

    思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是 ...

  9. ​网页图表Highcharts实践教程标之加入题副标题版权信息

    ​网页图表Highcharts实践教程标之加入题副标题版权信息 Highcharts辅助元素 辅助元素图表的非必要元素.如标题.版权信息.标签.加载动态.它们不和图表数据发生关联,仅仅是额外说明一些基 ...

  10. Restful --- 让JSON回归单纯

    设计模式才是软件哲学的根本.. 一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓 ...