tags:

views:

1919

answers:

4

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

+3  A: 

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>
Jose Basilio
I'm very new to all this, if there is a better way of doing this, I'm open to suggestions.
xtrabits
By adding the asterisk as shown above, do you see it when you click your submit button and the text exceeds 200 characters?
Jose Basilio
+2  A: 

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;

Genady Sergeev
Thanks, that's great.
xtrabits
by the way, instead of a customvalidator you can use RegularExpressionValidator with ValidateExpression set to "\w{1,200}" where \w means a word character and {1,200} means with length from 1 to 200 symbols.
Genady Sergeev
+1  A: 

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

if (Page.IsValid==false)
   return;

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

Khaled Musaied
+1  A: 

Remeber to set this properrty on the CustomValidator...

ValidateEmptyText="True"
RemotecUk