我正在尝试触发ASP.NET必需字段验证器,如下所示。除非并且直到所需的字段验证器返回true,否则我不希望触发JavaScript函数。在我的情况下,我按下按钮点击它并没有返回任何内容。
Asp.net代码
<div class="login_txt">
Email ID<span class="asterisk">*</span>:
</div>
<asp:TextBox ID="txtEmailId_ExistingUser" Style="width: 169px;" runat="server" ValidationGroup="login"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RFVtxtEmailId_ExistingUser" Font-Size="Smaller" ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic" ForeColor="Red" runat="server" ErrorMessage="Please enter Email-ID"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RevtxtEmailId_ExistingUser" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Font-Size="Smaller" ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic" ForeColor="Red" runat="server" ErrorMessage="Email-ID not in correct Format"></asp:RegularExpressionValidator>
</div>
<div id="f2" class="text" style="margin-bottom: 0px">
<div class="login_txt">
Password<span class="asterisk">*</span> :
</div>
<asp:TextBox ID="txtPassword" runat="server" ValidationGroup="login" TextMode="Password"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RFVtxtPassword" Font-Size="Smaller" ControlToValidate="txtPassword" Display="Dynamic" ForeColor="Red" runat="server" ErrorMessage="Please Enter Password"></asp:RequiredFieldValidator>
</div>
</div>
<div class="h5"></div>
<div id="f3" style="padding-left: 115px; padding-top: 10px">
<asp:ImageButton ID="imgbtn_login" runat="server" ValidationGroup="login" CssClass="login_btn" ImageUrl="images/Reg-login.png" OnClientClick="return ValidateLogin();" />
</div>
的JavaScript
<script type="text/javascript" language="javascript">
function ValidateLogin(){
if (Page.IsValid) {
alert('valid');
}
else {
alert('Invalid');
}
}
</script>
答案 0 :(得分:2)
您可以在没有脚本的情况下处理它:通过提供
CausesValidation =&#34;真&#34;和ValidationGroup =&#34; someGroupName&#34;
到你的<asp:ImageButton/>
以及ValidationGroup =&#34; FormValidation&#34;`给你的验证人:
答案 1 :(得分:2)
根据您的HTML,我可以看到您没有提到两件事。
您没有将ValidationGroup="Login"
添加到验证控件
您尚未在按钮中添加CausesValidation="true"
属性
如需完整参考,请查看以下内容: -
<div class="login_txt">
Email ID<span class="asterisk">*</span>:</div>
<asp:TextBox ID="txtEmailId_ExistingUser" Style="width: 169px;" runat="server" ValidationGroup="login"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RFVtxtEmailId_ExistingUser" ValidationGroup="login"
Font-Size="Smaller" ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic"
ForeColor="Red" runat="server" ErrorMessage="Please enter Email-ID"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RevtxtEmailId_ExistingUser" ValidationGroup="login"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Font-Size="Smaller"
ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic" ForeColor="Red"
runat="server" ErrorMessage="Email-ID not in correct Format"></asp:RegularExpressionValidator>
</div>
</div>
<div id="f2" class="text" style="margin-bottom: 0px">
<div class="login_txt">
Password<span class="asterisk">*</span> :
</div>
<asp:TextBox ID="txtPassword" runat="server" ValidationGroup="login" TextMode="Password"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RFVtxtPassword" ValidationGroup="login" Font-Size="Smaller"
ControlToValidate="txtPassword" Display="Dynamic" ForeColor="Red" runat="server"
ErrorMessage="Please Enter Password"></asp:RequiredFieldValidator>
</div>
</div>
<div class="h5">
</div>
<div id="f3" style="padding-left: 115px; padding-top: 10px">
<asp:ImageButton ID="imgbtn_login" CausesValidation="true" runat="server" ValidationGroup="login"
CssClass="login_btn" ImageUrl="images/Reg-login.png" OnClientClick="return ValidateLogin();" />
</div>
希望能帮到你
看看下面的截图: -
答案 2 :(得分:1)
在每个asp Validator中提及ValidationGroup
,如下所示:
<div class="login_txt">Email ID<span class="asterisk">*</span>:</div>
<asp:TextBox ID="txtEmailId_ExistingUser" Style="width: 169px;" runat="server" ValidationGroup="login"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RFVtxtEmailId_ExistingUser" Font-Size="Smaller" ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic" ForeColor="Red" runat="server" ErrorMessage="Please enter Email-ID" ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RevtxtEmailId_ExistingUser" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Font-Size="Smaller" ControlToValidate="txtEmailId_ExistingUser" Display="Dynamic" ForeColor="Red" runat="server"
ErrorMessage="Email-ID not in correct Format" ValidationGroup="login"></asp:RegularExpressionValidator></div>
这会奏效。如果验证失败;那么只会调用JS
函数。