1、原生js写法
1
2
3
4
5
6
7
8
|
!function
()
{
document.getElementById('bankCard').onkeyup
=
function
(event)
{
var
v
=
this.value;
if(/S{5}/.test(v)){
this.value = v.replace(/s/g,
'').replace(/(.{4})/g,
"$1 ");
}
};
}();
|
2、jQuery写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!DOCTYPE html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
<title></title>
</head>
<body>
<input
type="text"
id="J_BankCard"/>
<script
src="http://static.ydcss.com/libs/jquery/1.11.2/jquery.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(/(.{4})/g,
"$1 "));
});
}();
</script>
</body>
</html>
|