Html
    Css
    Js
1
<input type="text" id="kahao">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$(function() {
$('#kahao').on('keyup', function(e) {
//
if ((e.which >= 48 && e.which <= 57) ||
(e.which >= 96 && e.which <= 105)) {
//
var caret = this.selectionStart
//value
var 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
}
})
})
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
↑上面代码改变,会自动显示代码结果 jQuery调用版本:1.11.3
 立即下载

银行卡四位空格

0