按下回车键时不会调用Javascript验证 [英] Javascript validation is not invoked when enter key is pressed
本文介绍了按下回车键时不会调用Javascript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有aspx格式的问题我有txtfeildname文本框,下面我有创建按钮。如果用户输入任何特殊字符并尝试单击创建按钮,则会提示用户您应该只输入字母 - 这在使用和单击鼠标时效果很好但是当用户点击键盘输入键时它不起作用
以下是我的代码
Hi, I have issue in aspx form i have txtfeildname a textbox and below that i have create button. if user enters any special characters and tries to click the createbutton it prompts the user you should enter only alphabets-- This works perfectly when mouse is used and clicked but it doesn't work when user clicks on enter key from keyboard
below is my code
<script language="javascript" type="text/javascript">
function populate() {
if (Validate() == false) return false;
return (confirm('Are you sure you want to Submit?'));
}
function Validate() {
if (validateTextBox('<%=txtTableName.ClientID%>', '<%=lblErrorTableName.ClientID%>', ' Only alphabets, numbers are allowed') == false) return false;
}
function ValidateListBox() {
if (ValidListBox('<%=listColumns.ClientID%>') == false) return false;
}
function ValidateAddColumns() {
if (validateTextBox('<%=txtColumnName.ClientID%>', '<%=lblErrorListColumn.ClientID%>', ' Only alphabets, numbers are allowed') == false) return false;
}
</script>
Aspx页面代码
Aspx page code
<pre lang="HTML"><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="always">
<ContentTemplate>
<div>
<table border="2" align="center" width="500px">
<tr>
<td>
Select Table Type:
</td>
<td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="always">
<ContentTemplate>
<asp:DropDownList ID="ddlTableType" runat="server" CssClass="GridViewStyle" AppendDataBoundItems="true"
DataTextField="Table Type" DataValueField="id" OnSelectedIndexChanged="ddlTableType_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Text="---Select Table Type---" Value="0" />
<asp:ListItem Text="Test Data" Value="1" />
<asp:ListItem Text="Test Script Component" Value="2" />
<asp:ListItem Text="Test Script" Value="3" />
<%-- <asp:ListItem Text="Batch Script" Value="4" />--%>
</asp:DropDownList>
</td><td>
<asp:ListBox ID="listColumns" runat="server" Visible="false" Height="107px" Width="153px"
SelectionMode="Multiple"></asp:ListBox>
<asp:TextBox ID="txtColumnName" runat="server" MaxLength="20" Visible="false"></asp:TextBox>
<asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click"
Visible="false" CssClass="btn"/>
<asp:Button ID="btnrevome" runat="server" Text="Remove" Height="26px"
Visible="false" OnClick="btnrevome_Click" CssClass="btn"/>
<asp:Label ID="lblErrorListColumn" runat="server" BackColor="White"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlTableType" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
Table Name:
</td>
<td>
<asp:TextBox ID="txtTableName" runat="server" Width="247px" MaxLength="20"></asp:TextBox>
<font size="1" color="red">(Max 20 Characters)</font>
</td>
<td>
<asp:Label ID="lblErrorTableName" runat="server" BackColor="White"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" CssClass="btn"/>
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" CssClass="btn"/>
</td>
</tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnClear" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
推荐答案
HI ShaHam11,
这里有你做的细节它。在这里我演示了仅在输入印刷机上对特殊字符的验证。
HI ShaHam11,
here is the detail of hows you do it. In this i demonstrated a validation for special character on enter press only.
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function validateAddress(objID) {
var TCode = document.getElementById(objID).value;
if (/[^a-zA-Z0-9\-\/]/.test(TCode)) {
alert('Input is not alphanumeric');
return false;
}
return true;
}
function KeyDownValidation(e) {
if (e.keyCode == 13) {
alert('you pressed enter ^_^');
validateAddress('txt');
}
}
</script>
<input id="txt" type="text" onkeydown="KeyDownValidation(event);">
这篇关于按下回车键时不会调用Javascript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文