<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 数字输入控制,给数字加上千分符 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style>
.inputRight {
text-align: right;
border: 1px #666666 dashed;
font-size: 9pt;
color: #993366;
height: 18px;
ime-mode : disabled;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
/*
这些功能建议通过HTC来实现
*/
////////////
//数字输入控制
function FormatInput(dotlen)
{
var myEle=event.srcElement;
var myValue=String.fromCharCode(event.keyCode);
if (myEle.readOnly)
{
return;
}
switch(dotlen)
{
//原正则表达式(可以输入负数):/^[\-]?\d*\.?\d{0,0}$/
case 0:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,0}$/, myValue); break;}
case 1:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,1}$/, myValue); break;}
case 2:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,2}$/, myValue); break;}
case 3:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,3}$/, myValue); break;}
case 4:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,4}$/, myValue); break;}
case 5:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,5}$/, myValue); break;}
case 6:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,6}$/, myValue); break;}
case 7:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,7}$/, myValue); break;}
case 8:{event.returnValue = regInput(myEle, /^\d*\.?\d{0,8}$/, myValue); break;}
default :{event.returnValue = regInput(myEle, /^\d*\.?\d{0,0}$/, myValue); break;}
}
}
function regInput(obj, reg, inputStr)
{
var docSel = document.selection.createRange()
if (docSel.parentElement().tagName != "INPUT") return false
oSel = docSel.duplicate()
oSel.text = ""
var srcRange = obj.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
return reg.test(str)
}
//给数字加上千分符 by yuanzy
function splitNumber(eValue)
{
var intPart = "";
var decPart = "";
if (eValue.indexOf(",")>=0)
{
eValue=eValue.replace(/,/g,"");
}
if (eValue.indexOf(".")>=0)
{
intPart=eValue.split(".")[0];
decPart=eValue.split(".")[1];
}
else
{
intPart = eValue;
}
var num = intPart+"";
var re=/(-?\d+)(\d{3})/
while(re.test(num)){
num=num.replace(re,"$1,$2")
}
if (eValue.indexOf(".")>=0)
{
eValue=num + "." + decPart;
}
else
{
eValue=num ;
}
return eValue;
}
function oFocus()
{
if (event.srcElement.readOnly) return;
event.srcElement.value=event.srcElement.value.replace(/,/g,"");
}
function oBlur()
{
if (event.srcElement.value=="-")
{
event.srcElement.value="";
}
event.srcElement.value = splitNumber(event.srcElement.value);
}
</SCRIPT>
</HEAD>
<BODY>
<input class="inputRight" type='text' onfocus="oFocus();" onblur="oBlur();" onKeypress="FormatInput(2);" >
</BODY>
</HTML>
posted on 2006-04-03 15:44
汪杰 阅读(379)
评论(0) 编辑 收藏 引用 所属分类:
hengxing网站js