我在gridview中有文本框。我需要验证这个文本框的数值。我尝试使用下面的代码,但它给我的对象需要error.my代码如下所示。
<asp:TabContainer ID="tcexe" runat="server" ActiveTabIndex="0" AutoPostBack="true">
<asp:tabpanel>
<asp:gridview>
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server" MaxLength="4" Text='<%# Eval("value") %>'
OnKeyPress=" return AllowNumericOnly(this);"> javascirpt used is function
</ItemTemplate>
</asp:TenGrid>
</asp:tabpanel>
AllowNumericOnly(e)
{
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (event)
keycode = event.keyCode;
else if (e)
keycode = e.which;
else return true;
if ((keycode > 47 && keycode <= 57)) { return true; }
else { return false; } return true;
}
我需要在Mozilla和IE中支持此功能。任何人都可以帮助我
答案 0 :(得分:0)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
<script type="text/javascript">
function isNumber(e) {
alert(e);
//condition returns true when shift + tab or tab key is pressed
if((e.shiftKey && e.keyCode == 9) || (e.keyCode == 9)) {
return true;
}
//for other conditions the shift key is disabled
if(e.shiftKey) {
return false;
}
var keynum = '';
// fetching the key code for IE
if(window.event) {
keynum = e.keyCode;
}
// fetching the key code for Netscape/Firefox/Opera
else if(e.which) {
keynum = e.which;
}
// 48-57 = 0-9, backspace = 8, tab = 9, 96 -105 = Numpad Digits
// F1-F12 = 112 - 123, arrows = 37 - 40
if ((keynum >= 48 && keynum <= 57)
|| keynum == 8
|| keynum == 9
|| (keynum >= 112 && keynum <= 123)
|| (keynum >= 37 && keynum <= 40)
|| (keynum >= 96 && keynum <= 105)
|| keynum == 46) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
Test No. <input type="text" id="testId" onkeydown="return isNumber(event);"></input>
</body>
</html>
答案 1 :(得分:-1)
<ItemTemplate>
<asp:TextBox ID="txtObtainMarks" runat="server" Width="50" MaxLength="2" OnKeyPress=" return AllowNumericOnly(this);"></asp:TextBox>
</ItemTemplate>
<script type="text/javascript" charset="utf-8">
function AllowNumericOnly(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (event)
keycode = event.keyCode;
else if (e)
keycode = e.which;
else return true;
if ((keycode > 47 && keycode <= 57)) { return true; }
else { return false; } return true;
}
</script>