js实现文本框只能输入数字
<script language="javascript" type="text/javascript">
function checkNumber(e,strid,msg)
{
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
var el = document.getElementById(strid);
var msg = document.getElementById(msg);
reg = /\d/;
var result = reg.test(keychar);
if(!result)
{
el.className = "warn";
msg.innerHTML = "只能输入数字";
return false;
}
else
{
el.className = "";
msg.innerHTML = "";
return true;
}
}
</script>
调用
<input type="text" id="gNum" onkeypress="return checkNumber(event,'gNum','gmsg');" style="height:20px; width:150px;"/><span id="gmsg" style="color:Red;"></span>
方法2:
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="50px" onkeypress="return isNumberKey(this)"></asp:TextBox>
<script language= "javascript" type="text/javascript">
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
return false;
}
return true;
}
</script>