Question

I have a custom validation for the name field of my site. In the code behind is the code for it but it doesn't work. It does not give the error like the other validators do. this is my code.

<asp:CustomValidator ID="CustomValidatorVoorNaam" runat="server" ControlToValidate="voornaamTextBox" onServerValidate="naamValidator" Display="Dynamic" ErrorMessage="Gebruik enkel het alfabet" ValidationGroup="AllValidators">Enkel A-Z</asp:CustomValidator>

protected void naamValidator(object sender, ServerValidateEventArgs e)
{
    bool check = true;
    for (int i = 0; i > e.ToString().Length; i++)
    {
        if (!(char.IsLetter(e.ToString()[i])))
        {
            check = false;
        }
    }
    e.IsValid = check;
}

as far as I could find this is the correct code.

UPDATE

The error is shown when I enter it while leaving it empty

Était-ce utile?

La solution

I guess e.toString() is going to return the type of e as string, try using value instead

protected void naamValidator(object sender, ServerValidateEventArgs e)
    {
        bool check = true;
        for (int i = 0; i > e.Value.Length; i++) 
        {
            if (!(char.IsLetter(e.ToString()[i])))
            {
                check = false;
                break;
            }
        }
        e.IsValid = check;

    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top