如何只显示按钮,点击后经常EX pression确认的消息? [英] How to show regular expression validation's message after button click only?

查看:129
本文介绍了如何只显示按钮,点击后经常EX pression确认的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的html code。我有一个电子邮件文本框,有一个登录按钮。我加入的电子邮件文本框为必填字段验证和定期EX pression验证。

Below is my html code. I have a email textbox and there is a login button. I have added a required field validator and regular expression validator for email textbox.

问题是,当我输入电子邮件文本浏览器的自动建议一些事情显示邮件列表一些。当我使用向下箭头键选择任何这些邮件,并且回车键它显示常规的前pression验证错误消息,即使邮件是正确的格式。

The problem is that when I type some thing in the email textbox browser's auto suggestion shows some list of emails. When I select any of those emails by using down arrow key and enter key it shows the error message for regular expression validation even though email is in proper format.

<asp:RequiredFieldValidator ID="reqValUserName" runat="server" 
    ErrorMessage="Email is required!" 
    ControlToValidate="txtUserName"
    ValidationGroup="validateCredential"
    Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regValUserName" runat="server" 
    ErrorMessage="Incorrect format!"
    ControlToValidate="txtUserName" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    ValidationGroup="validateCredential"
    Display="Static">
</asp:RegularExpressionValidator>                 

     <asp:TextBox ID="txtUserName" runat="server"
           TabIndex="1" CssClass="inputCredential" MaxLength="60"
           AccessKey="E" 
           ValidationGroup="validateCredential">
     </asp:TextBox>

     <asp:Button ID="btnLogin" runat="server" CssClass="btnPrimary" 
           Text="Login" onclick="btnLogin_Click" 
           ValidationGroup="validateCredential"/>

在这个形象,你看,如果我选择的建议和preSS的电子邮件中输入它显示了错误的电子邮件验证消息。

In this image as you see if I select the email from the suggestion and press enter it is showing the wrong email validation message.

任何人都可以请让我知道,如何制止这种消息显示?

Can anyone please let me know, how to stop this kind of message display?

如果有关于这个问题需要澄清,那么请把它添加注释。

If there is any clarification needed regarding the question then please add it as a comment.

推荐答案

感谢您的解答和建议。
下面是我通过所有的答案会后进行。

Thanks for all your answers and suggestions. Below is what I have done after going through all the answers.

<asp:RegularExpressionValidator ID="regValUserName" runat="server" 
     ErrorMessage="Incorrect format!"
     ControlToValidate="txtUserName" 
     ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
     ValidationGroup="validateCredential"
     Display="Dynamic" EnableClientScript="false">
</asp:RegularExpressionValidator>

正如你所看到的,我已经加入EnableClientScript =假,以便当我输入电子邮件的一半,从自动建议和preSS选择将不会显示错误信息输入。

As you can see, I have added EnableClientScript="false" so that the error message will not be shown when I type half of email and select from auto suggestion and press enter.

现在的问题是它总是检查输入的证书是否正确,不是因为它是在服务器端做验证。所以我有一些不必要的code执行与数据库交互。

Now the problem was it was always checking for whether entered credentials are correct of not as it was doing validation in server side. So I had some unnecessary code execution and database interaction.

因此​​,在登录按钮的Click事件处理程序我做了如下改变。

So in click event handler of login button I did following change.

if(Page.IsValid)
{
     // My credential check code
}

所以,code以上块将运行code检查输入的证书的正确性,只有当他们在正确的格式。

So the above block of code will run code for checking correctness of entered credentials only if they are in proper format.

但我仍然在寻找更好的答案。这只是一个变通。因为当它涉及到高性能服务器端验证,则不能使用客户端验证。在这里,我与实用性妥协。因为我想给用户后,立即他被通知/她输入了错误格式的电子邮件。这可以通过使用JavaScript来实现,但我不知道是否有什么办法,我们可以使用验证器控件实现它。

But I am still looking for a better answer. This is only a work around. Because when it comes to performance server side validation can never match client side validation. Here I am compromising with usability. As I want user to be notified immediately after he/she enters a wrong formatted email. This can be achieved by using javascript, but I wonder if there is any way we can achieve it using validator controls..

这篇关于如何只显示按钮,点击后经常EX pression确认的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆