开发者

Trigger the status of the client side validation

I have a .net form with several text Fields and some of them are ma开发者_StackOverflowrked 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()


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().


It is better to use custom validator in this case and make clientvalidation function and do your work if values provided


When you include asp .net validators onto your aspx page then a series of validation scripts are automatically included on your client side rendered page. These scripts include a function called "Page_ClientValidate" which when called checks the validator state for those validators which support client side checking.

You can check the client side validation state using the following;

var validPage = Page_ClientValidate();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜