<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>金融软件文本框禁用非法字符特效-2011-04-26</title>
<style type="text/css">
.ime
{
ime-mode: disabled; /**只对IE有效,禁用输入法切换**/
width:300px;
}
</style>
<script language="javascript" type="text/javascript">
function isValiNum(k) {
return (k == 9 || k == 13 || k == 46 || k == 8 || k == 189 || k == 190 || k == 110 || (k >= 48 && k <= 57) || (k >= 96 && k <= 105) || (k >= 37 && k <= 40));
}
function numKeyDown() {
var k = window.event.keyCode;
if(k==86) {
return numPaste();
}
//判断输入字符是否合法;调用isValiNum函数;
return isValiNum(k);
}
function numPaste() {
//从剪切板中获取数据;
var text = window.clipboardData.getData("Text");
for (var i = 0; i < text.length; i++) {
var asc = text.charCodeAt(i); //转成ASCII值;
if (!isValiNum(asc)) {
alert("您的粘贴内容有非法字符");
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, "{1},") + s2; });
return n1;
}
</script>
</head>
<body>
<input id="txtCode" type="text" class="ime" οnpaste="return numPaste()" οnkeydοwn="return numKeyDown()" οnfοcus="this.style.textAlign='right'; this.value=this.value.replace(/,/g,'');" οnblur="this.style.textAlign='right';this.value=commafy(this.value)" />
</body>
</html>
JS金融文本框
最新推荐文章于 2024-08-11 14:52:10 发布