我有一个aspx内容页面,这是由母版页继承的。
在母版页中,我的主要内容占位符位于<form>
,
<form runat="server">
<div class="page">
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
以下是我的内容页面的代码:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table border="0" cellpadding="3" cellspacing="3">
<tr>
<td colspan="2" class="header" style="color: Black;">
LOGIN USER
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblUserName" runat="server" Text="UserName: "></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblPassword" runat="server" Text="Password: "></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:Button ID="btnReset" runat="server" Text="Reset" />
</td>
</tr>
</table>
<div align="center" class="alertmsg">
</div>
我将使用jQuery验证插件:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#ctl01').validate({
rules: {
<%=txtUserName.UniqueID %>: {
minlength: 2,
required: true
},
<%=txtPassword.UniqueID %>: {
required: true,
email:true
}
},
messages: {
<%=txtUserName.UniqueID %>:{
required: "* Required Field *",
minlength: "* Please enter atleast 2 characters *"
},
<%=txtPassword.UniqueID %>:{
required: "* Required Field *",
email: "* Please enter valid email address *"
}
}
});
});
</script>
txtUserName
可以公开<%=txtUserName.UniqueID %>
的动态ID,但为什么我需要将表单的ID提供给$('#ctl01').validate
?
我可以使用任何其他ID代替#ctl01
吗?
答案 0 :(得分:0)
您可以使用:注意$('<%=Form.UniqueID %>').validate({
和$('<%=Form.UniqueID %>').validate({
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#<%=Form.UniqueID %>').validate({
rules: {
<%=txtUserName.UniqueID %>: {
minlength: 2,
required: true
},
<%=txtPassword.UniqueID %>: {
required: true,
email:true
}
},
messages: {
<%=txtUserName.UniqueID %>:{
required: "* Required Field *",
minlength: "* Please enter atleast 2 characters *"
},
<%=txtPassword.UniqueID %>:{
required: "* Required Field *",
email: "* Please enter valid email address *"
}
}
});
});
</script>
答案 1 :(得分:0)
.validate()
要求它来自,因为您在发送到服务器之前进行验证以检查是否正确值,即当用户提交表单时,只能提交表单,并且只能通过他们提交可以防止错误的值开始提交..
更清楚:
您可以参考http://docs.jquery.com/Plugins/Validation/validate进行明确说明。
如果Control.UniqueID
不起作用您可以使用Control.ClientID
...