javascript控制text只能输入数字(整数,小数),兼容ie,ff,safari【转载】
http://hi.baidu.com/aaxh/blog/item/804b2ef5d22e2a27bd310951.html
<SCRIPT LANGUAGE="JavaScript">
<!--
var isIE = false;
var isFF = false;
var isSa = false;
if ((navigator.userAgent.indexOf("MSIE")>0) && (parseInt(navigator.appVersion) >=4))isIE = true;
if(navigator.userAgent.indexOf("Firefox")>0)isFF = true;
if(navigator.userAgent.indexOf("Safari")>0)isSa = true;
function onlyNumber(e)
{
var key;
iKeyCode = window.event?e.keyCode:e.which;
if( !(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 13) || (iKeyCode == 46) || (iKeyCode == 45) || (iKeyCode == 37) || (iKeyCode == 39) || (iKeyCode == 8)))
{
if (isIE)
{
e.returnValue=false;
}
else
{
e.preventDefault();
}
}
}
//-->
</SCRIPT>
<input type="text" onkeypress="return onlyNumber(event)">
另外一个:
=====================
<input type="text" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');if(this.value.split('.').length>2){this.value=this.value.split('.')[0]+'.'+this.value.split('.')[1]}">
<SCRIPT LANGUAGE="JavaScript">
<!--
var isIE = false;
var isFF = false;
var isSa = false;
if ((navigator.userAgent.indexOf("MSIE")>0) && (parseInt(navigator.appVersion) >=4))isIE = true;
if(navigator.userAgent.indexOf("Firefox")>0)isFF = true;
if(navigator.userAgent.indexOf("Safari")>0)isSa = true;
function onlyNumber(e)
{
var key;
iKeyCode = window.event?e.keyCode:e.which;
if( !(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 13) || (iKeyCode == 46) || (iKeyCode == 45) || (iKeyCode == 37) || (iKeyCode == 39) || (iKeyCode == 8)))
{
if (isIE)
{
e.returnValue=false;
}
else
{
e.preventDefault();
}
}
}
//-->
</SCRIPT>
<input type="text" onkeypress="return onlyNumber(event)">
另外一个:
=====================
<input type="text" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');if(this.value.split('.').length>2){this.value=this.value.split('.')[0]+'.'+this.value.split('.')[1]}">