xtrabits
xtrabits

Reputation: 55

Custom Validator validation not firing for textbox

I have created a custom validator for maximum characters for a multiline textbox in ASP.Net.

Below is the code that I am using.

<asp:CustomValidator ID="cvPersonality" runat="server" ControlToValidate="txtPersonality"
    Display="Dynamic" ErrorMessage="*Maximum Characters 200" 
    OnServerValidate="cvPersonality_ServerValidate"></asp:CustomValidator>

Protected Sub cvPersonality_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    Dim strDesc As String = Me.txtPersonality.Text
    If Len(strDesc) > 200 Then
     args.IsValid = False
    Else
     args.IsValid = True
    End If
End Sub

Thanks

Upvotes: 1

Views: 25571

Answers (4)

Remotec
Remotec

Reputation: 10772

Remember to set this property on the CustomValidator...

ValidateEmptyText="True"

Upvotes: 28

Khaled Musaied
Khaled Musaied

Reputation: 2493

you have to add the following code to your submission button:

if (!Page.IsValid)
   return;

the custom validation is by default a server validation control and you have to stop processing if the page validators are not valid.

Upvotes: 4

Genady Sergeev
Genady Sergeev

Reputation: 1650

You can use the following code for a client side validation, as a complement to the server-side one. The client side validation is helpful in some simple cases ( like max text length ) because it reduces the server overhead, there is no redundant postback just for a simple check. EXample code:

 <script type="text/javascript">
    function clientValidate(sender, args) {
        if (args.Value.length > 200) {
            args.IsValid = false;
        }
    }
</script>

<div>
    <asp:TextBox runat="server" ID="TextBox1" TextMode="MultiLine"></asp:TextBox>
    <asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="TextBox1"
        Text="The text length exceeds the allowed maximum" 
        ClientValidationFunction="clientValidate" Display="Dynamic">
    </asp:CustomValidator>
    <asp:RequiredFieldValidator runat="server" ID="ReqFieldValidator1" ControlToValidate="TextBox1"
        Text="You must enter a text!" Display="Dynamic">
    </asp:RequiredFieldValidator>
    <asp:Button runat="server" ID="Button1" Text="Postback" CausesValidation="true" />
</div>

Please, pay attention to the RequiredFieldValidator, this is to make sure that there is entered text in the textbox. For some reason, the CustomValidator does not catch when args.Value.length == 0;

Upvotes: 6

Jose Basilio
Jose Basilio

Reputation: 51548

First of all, how do you know that the validator is not firing. Did you debug the app and checked whether the cvPersonality_ServerValidate() method is being called?

Second, you don't have a client side validator and therefore, it will only fire when there's a postback.

Third, looking at the markup, it looks like you only have the ErrorMessage property set up. That only displays if you have a ValidationSummary control on the page. You should also set the text property or have something to display inside the markup tag for the validator, usually an asterik as shown below:

<asp:CustomValidator ID="cvPersonality" runat="server" 
    ControlToValidate="txtPersonality"
    Display="Dynamic" ErrorMessage="*Maximum Characters 200" 
    OnServerValidate="cvPersonality_ServerValidate">*</asp:CustomValidator>

Upvotes: 3

Related Questions