简体   繁体   中英

How do I fire an ASP.NET custom validator from JavaScript?

我想在javascript中编写一个函数,每次我在webform中调用它时,它会在同一个webform中激活一个特定的验证器。

<asp:RequiredFieldValidator ID="passwordRequiredFieldValidator" runat="server" 
    ErrorMessage="* Password Required!"
    EnableClientScript="true" CssClass="errorText" 
    ControlToValidate="passwordTextBox"> 
</asp:RequiredFieldValidator>

To fire this validator use:

window.ValidatorValidate(window.passwordRequiredFieldValidator);

Further example:

function ValidatePassword() {
    window.ValidatorValidate(window.passwordRequiredFieldValidator);
    var valid = window.passwordRequiredFieldValidator.isvalid;
    alert(valid);
}
if (Page_ClientValidate('Validation Group'))
{
    //Your valid
}

The "CustomValidator" control lets you use javascript to validate your form. If you do this you should also do the same validation on the server so that the user can't just disable javascript to bypass the validation errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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