Robin
Robin

Reputation: 2723

custom validator not showing errors

I have an ASP.NET webform. All of my validators do their job except for the custom validator. As far as I know my code is correct but he doesn't show the errors. It seems like he doesn't call my javascript function or my code behind function.

here is my code

javascript

<%-- javascript--%>
<script type="text/javascript" >
    function validateNaam(oSrc, args) {
        var voorNaam = document.getElementById("Voornaam");;
        var regex = /^[a-zA-Z ]{2,30}$/;

        if (voorNaam.match(regex)) {
            alert("Gelieve geen ongeldige tekens te gebruiken voor uw naam aub.");
            return false;
        }
    }

</script>

aps custom validator

 <%-- Gebruikersnaam label, textbox en validator --%>&nbsp;<asp:Label ID="Label1" runat="server" AssociatedControlID="Email">Voornaam: </asp:Label>
                            <asp:TextBox runat="server" ID="Voornaam" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Voornaam"
                                CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                            <%-- Custom validator voor naam --%>
                            <asp:CustomValidator ID="CustomValidatorGebruikersnaam" runat="server" 
                                CssClass="field-validation-error" ErrorMessage="Uw naam bevat ongeldige tekens" 
                                ValidationGroup="AllValidators" OnServerValidate="CustomValidatorGebruikersnaam_ServerValidate"
                                ClientValidationFunction="validateNaam" ControlToValidate="Voornaam">!!</asp:CustomValidator>

and code behind

protected void CustomValidatorGebruikersnaam_ServerValidate(object source, ServerValidateEventArgs args)
{
    try
    {
        args.IsValid = Validatie.ControleerNaam(args.Value.ToString());
    }
    catch 
    {

        args.IsValid = false;
    }

}

Upvotes: 1

Views: 2607

Answers (1)

hutchonoid
hutchonoid

Reputation: 33306

You have a validation group on your custom validator.

The code for the button, which you have not posted must have no validation group on it.

Remove this ValidationGroup="AllValidators" and it should work.

Upvotes: 1

Related Questions