禁止文本框输入字母

<script language="javascript">
function ANP_keydown(e)
{
var kcode;
if(window.event)//for ie
{
kcode=e.keyCode;
}
else if(e.which)//for not ie
{
kcode=e.which;
}

var validKey=(kcode==8||kcode==46||kcode==37||kcode==39||(kcode>=48&&kcode<=57)||(kcode>=96&&kcode<=105));

if(!validKey)
{
if(kcode==13){
alert("do something");
}
if(e.preventDefault)//for not ie
{
e.preventDefault();
}
else
{
event.returnValue=false// for ie
};
}
}
</script>

<input type="text" onkeydown="ANP_keydown(event);"> 

你可能感兴趣的:(JavaScript,职场,休闲)