<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>会计金额输入</title>
<script type="text/javascript">
function numKeyDown(src){
var k = window.event.keyCode;
//不能有两个 .
if((k==110 || k == 190) && src.value.indexOf(".")>=0){
return false;
}
//只能有一个 -(负号),且只能为第一个字符
if((k==109 || k==189) && src.value.length != 0){
return false;
}
//判断 key 是否为合法的 ASCII
if( k==8 || k==9 || k==13 || k==46 || k==109 || k==189 || k==190 || k==110 ||(k>=48 && k<=57) || (k>=96 && k<=105) || (k>=35 && k<=40)){
}else{
return false;
}
}
function isValidNum(k){
return ( k==8 || k==9 || k==13 || k==46 || k==109 || k==189 || k==190 || k==110 ||(k>=48 && k<= 57) || (k>=96 && k<=105) || (k>=35 && k<=40));
}
//粘贴时验证数据是否合法
function numPaste(src){
var text = src.value + clipboardData.getData("Text");
for(var i=0; i<text.length; i++){
var asc = text.charCodeAt(i);//取得字符的 ASCII 码
if(!isValidNum(asc)){
return false;
}
}
}
//附加千分位
function commafy(n){
var re = /\d{1,3}(?=(\d{3})+$)/g;
var n1 = n.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,") + s2;});
return n1;
}
</script>
<style type="text/css">
</style>
</head>
<body>
<input type="text" onpaste="return numPaste(src);" style="ime-mode:disabled" onkeydown="return numKeyDown(this);" onblur="this.style.textAlign='right'; this.value=commafy(this.value);" onfocus="this.style.textAlign='left'; this.value=this.value.replace(/,/g,'');" />
</body>
</html>