检查ASP.Net验证器客户端的结果 [英] Check result of ASP.Net validator clientside
问题描述
我知道内置的ASP.Net验证器带有客户端框架,但是我找不到任何可以检查单个验证器有效状态的东西.
I know the built-in ASP.Net validators come with a client-side framework, however I've been unable to find anything that lets me check a single validator for it's Valid state.
我希望这是可能的,所以我希望这里的人知道如何做:-)
I expect it to be possible though, so I hope someone in here knows how to do it :-)
有问题的验证器是RegularExpressionValidator,我用它来确定电子邮件地址是否有效.
The validator in question is a RegularExpressionValidator, which I use to determine whether an e-mail address is valid or not.
下面是一些简短的代码:
Here's some brief code:
<script>
function CheckForExistingEmail()
{
Page_ClientValidate(); // Ensure client validation
if (revEmail.IsValid) // pseudo code!
{
// Perform server side lookup in DB for whether the e-mail exists.
}
}
</script>
<asp:TextBox runat="server" id="tbEmail" onblur="CheckForExistingEmail();" />
<asp:RegularExpressionValidator id="revEmail" runat="server" ControlToValidate="tbEmail" ErrorMessage="Not a valid e-mail address" ValidationExpression="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})" />
推荐答案
我现在自己找到了解决方法:
I found a way around it myself now:
通过将ValidationGroup添加到验证器,我可以使用Page_ClientValidate(validationgroup)-返回布尔值.
By adding a ValidationGroup to the validator, I can use Page_ClientValidate(validationgroup) - which returns a bool value.
我不确定这与Pabuc的意思是否相同,如果是请放弃答案,我显然会选择它作为正确的答案:-)
I'm not sure if it was the same thing you meant Pabuc, if it was please drop an answer and I'll obviously select that as the correct one :-)
这是有效的代码:
<script>
function CheckForExistingEmail()
{
if(Page_ClientValidate("email"))
{
// Perform server side lookup in DB for whether the e-mail exists.
}
}
</script>
<asp:TextBox runat="server" id="tbEmail" onblur="CheckForExistingEmail();" />
<asp:RegularExpressionValidator id="revEmail" runat="server" ValidationGroup="email" ControlToValidate="tbEmail" ErrorMessage="Not a valid e-mail address" ValidationExpression="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})" />
这篇关于检查ASP.Net验证器客户端的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!