David Shochet
David Shochet

Reputation: 5385

How to run validator from javascript?

Here is a part of my code:

<asp:ListBox ID="lbRD" runat="server" DataSourceID="RDSqlDataSource" onchange="JSFillDetail();" DataTextField="Описание" DataValueField="ID" Width="188px" Height="200px"/>

<asp:TextBox ID="txtDescription" runat="server" />

<asp:RequiredFieldValidator ID="txtDescriptionRequiredFieldValidator" runat="server" ErrorMessage="Описание является обязательным для заполнения" ControlToValidate="txtDescription" />

I have a listbox, a textbox and a required field validator on my page. When the user selects something from the listbox, the selected item appears in the textbox using a javascript function. When the page is submitted, the validator reports an error in case the textbox is empty. If after that the user selects something from the listbox, the error message is still displayed, even though the textbox is not empty anymore. How can I make the validator validate the textbox, or even better, to clear the error message from the javascript function that fills the textbox? Thanks, David

Upvotes: 1

Views: 998

Answers (1)

rick schott
rick schott

Reputation: 21127

You can control your page flow with the Page_ClientValidate() function and the Page_Validators collection:

Page_ClientValidate();
var i;
for (i = 0; i < Page_Validators.length; i++)
{
    if (!Page_Validators[i].isvalid)
    {
        //example: call focus on first item that's invalid
        document.getElementById(Page_Validators[i].controltovalidate).focus();
        break;
    }
}

Upvotes: 5

Related Questions