简体   繁体   English

触发验证器使用js验证控件

[英]Triggering Validators validating a control using js

I have a setup similar to this: 我有一个与此类似的设置:

<asp:TextBox ID="txtEmail" runat="server"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtEmail" ErrorMessage="Required field cannot be left blank."
    Display="Dynamic"/>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
    ErrorMessage="Invalid email address."    ControlToValidate="txtEmail" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    Display="Dynamic"/>

Now, I'm running some sort of js code which will change the value of the textbox, and I'd like to force the validators to trigger again. 现在,我正在运行某种js代码,这些代码将更改文本框的值,并且我想强制验证器再次触发。 The JS code I'm using is intended to be modular, so when it triggers I don't know the name of the validation group, whether the control has a validator attached to it or whatever. 我正在使用的JS代码是模块化的,因此当它触发时,我不知道验证组的名称,控件是否附加了验证器或其他。

I've ended up using 我最终使用了

Page_ClientValidate()

But the page itself is going to have more than one validator, and I don't want them all the pop up at the same time. 但是页面本身将有多个验证器,我不希望它们同时弹出。

So the basic question is - is there a way that I can get a list of validators 'related' to a control, and force them to revalidate the input? 因此,基本问题是-有没有办法我可以获取与控件“相关”的验证器列表,并强迫他们重新验证输入?

(To clarify - the code I posted above is just to explain my problem better. It's not the actual code I'm using) (为澄清起见,我上面发布的代码只是为了更好地说明我的问题。这不是我正在使用的实际代码)

You can associate the email validators with a validation group (for example "emailValidationGroup") : 您可以将电子邮件验证程序与验证组关联(例如“ emailValidationGroup”):

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtEmail" ErrorMessage="Required field cannot be left blank."
    Display="Dynamic" ValidationGroup="emailValidationGroup"/>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
    ErrorMessage="Invalid email address."    ControlToValidate="txtEmail" 
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
    Display="Dynamic" ValidationGroup="emailValidationGroup"/>

and then call : Page_ClientValidate("emailValidationGroup"); 然后调用: Page_ClientValidate("emailValidationGroup");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM