ASP.NET Custom Validator Client side & Server Side validation not firing

前端 未结 7 1578
梦毁少年i
梦毁少年i 2020-12-12 20:16

This has not happened to me before, but for some reason both the client and server side validation events are not being triggered:



        
相关标签:
7条回答
  • 2020-12-12 20:47

    Use this:

    <asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>
    

    To validate an empty field.

    You don't need to add 2 validators !

    0 讨论(0)
  • 2020-12-12 20:52

    Your CustomValidator will only fire when the TextBox isn't empty.

    If you need to ensure that it's not empty then you'll need a RequiredFieldValidator too.

    Note: If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

    EDIT:

    If your CustomValidator specifies the ControlToValidate attribute (and your original example does) then your validation functions will only be called when the control isn't empty.

    If you don't specify ControlToValidate then your validation functions will be called every time.

    This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator, you could omit the ControlToValidate attribute from the CustomValidator and setup your validation functions to do something like this:

    Client Side code (Javascript):

    function TextBoxDCountyClient(sender, args) {
        var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
        if (v == '') {
            args.IsValid = false;  // field is empty
        }
        else {
            // do your other validation tests here...
        }
    }
    

    Server side code (C#):

    protected void TextBoxDTownCity_Validate(
        object source, ServerValidateEventArgs args)
    {
        string v = TextBoxDTownCity.Text;
        if (v == string.Empty)
        {
            args.IsValid = false;  // field is empty
        }
        else
        {
            // do your other validation tests here...
        }
    }
    
    0 讨论(0)
  • 2020-12-12 20:54

    Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?

    I'm not sure what else might cause this behavior.

    0 讨论(0)
  • 2020-12-12 20:58

    Client-side validation was not being executed at all on my web form and I had no idea why. It turns out the problem was the name of the javascript function was the same as the server control ID.

    So you can't do this...

    <script>
      function vld(sender, args) { args.IsValid = true; }
    </script>
    <asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />
    

    But this works:

    <script>
      function validate_vld(sender, args) { args.IsValid = true; }
    </script>
    <asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />
    

    I'm guessing it conflicts with internal .NET Javascript?

    0 讨论(0)
  • 2020-12-12 21:00

    Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via

     Page.Validate({Insert validation group name here});
    
    0 讨论(0)
  • 2020-12-12 21:07

    Thanks for that info on the ControlToValidate LukeH!

    What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.

    Cheers!

    0 讨论(0)
提交回复
热议问题