Trigger the status of the client side validation

asp.netrequiredfieldvalidatorvalidation

I have a .net form with several text Fields and some of them are marked with the RequiredFieldValidator.

<asp:TextBox MaxLength="150" Width="300" runat="server" ID="CityTXT"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="CityTXT"
                                    ValidationGroup="PersoanlDetailsVG" runat="server" ErrorMessage="Enter City">*</asp:RequiredFieldValidator>

I need to call a javascript function only if all these fields have values.

How could I trigger the status of the specific client side validation?

Thanks

EDIT

I tried to implement the Page_ClientValidate but the page doesn't postBack.

Here is my code

function Validate() {
        var res = Page_ClientValidate("PersoanlDetailsVG");

        if (res == true) {
            //do someting
        }
        return res;
    }

<asp:Button Visible="false"  CssClass="Proceedtopayment" runat="server"  OnClientClick="Validate();return false;" OnClick="ConfirmBooking" CausesValidation="true" ValidationGroup="PersoanlDetailsVG" />

It never executes the ConfirmBooking()

Best Answer

Have a look at the clientside validation API from ASP.NET.

You could check if Page_IsValid on clientside to detect if all validators are valid.

A boolean variable that indicates whether the page is currently valid. The validation scripts keep this up to date at all times.

For example:

if(Page_IsValid){
   foo();
}

The necessary WebUIValidation.js-file is automatically included if

  • The page contains one or more validation Web controls.
  • The page is being visited by an “uplevel” browser.

According to your updated answer: You page doesn't postback because you return false in all cases in onClientClick.

You must return the result of the validation if you want to postback on Button-Click if validation was successful:

OnClientClick="return Validate();"

The difference in @Brian's solution and mine is that Page_ClientValidate() is a function that triggers the validation(and internally returns Page_IsValid) while Page_IsValid only returns the state of the validation. Because the validation scripts keep Page_IsValid up to date at all times, it's redundant to call Page_ClientValidate().