效果类似 支付宝的这个 “信用卡号”输入框:(输入的时候就自动加空格 不是输完了才加)
https://ccrprod.alipay.com/ccr/billEnteringNoLogin.htm
他这个也有点小BUG!
我写的BUG更多! O(∩_∩)O~ 谢了!
解决方案 »
- 浏览器问题
- 使用jQuery.Validate插件,怎么能够不用submit按钮来触发它?
- function后的分号
- 如何删除图片上的DIV层,没有实现!!
- 从后台返回一个javascript函数,在页面调用的时候为什么总是说函数未定义??????
- 老问题新要求!(关于右键屏蔽) 高手请进!
- 初學者問個簡單的問題
- 救命!!大虾们!请进来!202.204.112.68的滚动新闻的实现?我并不想要新闻,只是想要一段文本!
- 如何实现将str=",01,02/,03,04" 分解
- zTree onClick事件没反应,已查阅很多资料,接近崩溃
- 在chrome下的window.showModalDialog问题
- extjs combo 问题,,
<script language="javascript">
<!--
var _length=0;
function test(){
var _o=document.getElementById("t");
_length++;
if(_length==4){
_o.value+=' ';
_length=0;
}
}
//-->
</script>
<input type="text" style="width:200px;height:25px;" onkeyup="test()" name="t" id="t" />
<html>
<head> <title>test</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("test").onkeyup = function() {
this.value = this.value.replace(/(\d{4})(?:\d)/g,"$1 ");
};
};
</script>
</head>
<body>
<input type="text" id="test" />
</body>
</html>
<html>
<head> <title>test</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("test").onkeyup = function() {
this.value = this.value.replace(/(\d{4})(?=\d)/g,"$1 ");
};
};
</script>
</head>
<body>
<input type="text" id="test" />
</body>
</html>
<script type="text/javascript">
function test(obj)
{
var value = obj.value;
value = value.replace(/\s*/g, "");
var result = [];
for(var i = 0; i < value.length; i++)
{
if (i % 4 == 0 && i != 0)
{
result.push(" " + value.charAt(i));
}
else
{
result.push(value.charAt(i));
}
}
obj.value = result.join("");
}
</script>
<input type="text" id="tt" onkeydown="test(this)" onblur="test(this)">
<html>
<head>
<script>
function a(Xtext)
{
var str=Xtext.value;
if(str.length<20)
{
if(str.length==4||str.length==9||str.length==14)
Xtext.value=Xtext.value+" ";
}
else
Xtext.value=Xtext.value.substring(0,Xtext.value.length-1);
}
</script>
</head>
<body>
<input type="text" id="text1" onkeyup="a(this)"/>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> <title>test</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("test").onkeyup = function() {
this.value = this.value.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 ");;
};
};
</script>
</head>
<body>
<input type="text" id="test" />
</body>
</html>
5楼的够用了另发个刚抠的建设银行用的 和5楼效果一样var str=(this.value).replace(/[^\d]/g, "");
var maxlen = 16;
if (str.length < maxlen) {
maxlen = str.length;
}
var temp = "";
for (var i = 0; i < maxlen; i++) {
temp = temp + str.substring(i, i + 1);
if (i != 0 && (i + 1) % 4 == 0 ) {
temp = temp + " ";
}
}
this.value=temp;
正则这样比较好:replace(/[^\d]/g, "") 让用户不能输入 数字以外的字符