js实现卡号每四位空格分隔
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实现卡号每四位空格分隔的更多相关文章
- js验证身份证号,超准确
js验证身份证号,超准确 看程序前先来了解下身份证号的构成:身份证号分为两种,旧的为15位,新的为18位.身份证15位编码规则:dddddd yymmdd xx p 其中 dddddd:地区码 ...
- sqlserver卡号段分组
之前给上海一家电子商务公司做一个卖卡系统,遇到了卡号段分组的问题.刚开始没什么好的实现方法,遂在博客园求助但未果,没法自己研究sql,终于搞定. 问题描述: 有个卡库存表,有个卡号字段,假设数据:16 ...
- C++中int转为char 以及int 转为string和string 转int和空格分隔字符串
1.对于int 转为char 直接上代码: 正确做法: void toChar(int b) { char u; ]; _itoa( b, buffer, ); //正确解法一 u = buffer[ ...
- 金蝶核算项目余额表卡号余额与天财商龙CRM卡号余额对比
金蝶核算项目余额表卡号余额与天财尚龙CRM卡号余额对比 由于历史遗留问题,财务一直不调账,修改核算科目卡号与天财商龙CRM系统一直,只能用VBA把卡号前缀修改成两边一致. 再通过,Power BI D ...
- highlight.js 设置行号
原文地址:highlight.js 设置行号 博客地址:http://www.extlight.com 一.背景 笔者在开发这套博客系统时使用 Editormd 作为 Markdown 编辑器,由于不 ...
- Linux下读取RFID卡号(C串口编程)
由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...
- .net第三方数据库物理卡号同步功能实现
本地数据库用的是Oracle,第三方数据库是SQL Server,连接字符串保存在web.config里面. 第三方数据库为增量,每次读取要记录读取的最大位置.我是保存在本地txt文件里面. //保存 ...
- Xamarin如何使用终端设备的NFC功能传递卡号等信息给Web页面(Android)
一.前提条件,App必须具有NFC权限. 二.项目中加入监控类NFCCatchActivity.cs [Activity(Label = "NFCCatch",Theme = &q ...
- vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)
自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...
随机推荐
- feignclient设置hystrix参数
序 feign默认集成了hystrix,那么问题来了,如何像hystrix command那样设置每个方法的hystrix属性呢. 实例 @FeignClient("product" ...
- ubuntu 12.04下gedit查看txt中文乱码解决办法
http://blog.sina.com.cn/s/blog_6273990801013dwv.html 由于我不能要求别人保存txt文件时必须用utf-8,那我只能自己找解决办法: 打开终端输入: ...
- 更改datatables的分页切换时的'processing'提示信息的式样
jquery.dataTables.css .dataTables_wrapper .dataTables_processing { position: absolute; top: 50%; lef ...
- 通过google浏览器的开发者工具修改cookie值
打开一个页面F12,然后再刷新下.点到如下图位置刚可以添加或重设cookie的值.修改cookie的值时,需要注意要清除下cookie.不然修改的cookie不生效.
- Workerman
What is it Workerman is a library for event-driven programming in PHP. It has a huge number of featu ...
- UC浏览器调试移动端网站
准备工作: UC浏览器开发版网址 UC浏览器开发者版下载地址 下载adb_tool 步骤: 1.将adb_tool解压,把里面的文件复制到 C:\Windows\SysWOW64 文件夹下面. 2.运 ...
- CSS圆角框,圆角提示框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [Cxf] spring-cxf 配置
1.在web.xml中配置servlet <!-- spring监听的配置 --> <listener> <listener-class>org.springfra ...
- UML总结---UML九种图关系说明
UML中包括九种图:用例图.类图.对象图.状态图.时序图.协作图.活动图.组件图.配置图. 1)用例图(Use Case Diagram) 它是UML中最简单也是最复杂的一种图.说它简单是因为它采用了 ...
- Storm快速理解
转自:http://blog.csdn.net/colorant/article/details/8256039 更多云计算相关项目快速理解文档 http://blog.csdn.net/color ...