Home > ASP.Net, Javascript > Using CustomValidator Control to Validate TextBox Length

Using CustomValidator Control to Validate TextBox Length


Hi all,

try this example to use CustomValidator Control to Validate TextBox Length

in this example i want to ensures a TextBox contains no more than 1000 words.

<asp:TextBox ID="txtText" runat="server" TextMode=" multiline" />
<asp:CustomValidator ID="cvText" ControlToValidate="txtText" runat="server" ErrorMessage="Your text is over 1000 words." OnServerValidate="cvtext_ServerValidate" ClientValidationFunction="cvText_ClientValidate" />

Using Server Side :-

protected void cvText_ServerValidate(object source, ServerValidateEventArgs args)
 {
     string[] numOfWords = args.Value.Split(new char[] { ' ' });
     args.IsValid = numOfWords.Length <= 1000;
 }

Using Client Side (Javascript) :-

function custStory_ServerValidate(source, args)
 {
     var numOfWords = args.Value.split(" ");
     args.IsValid = numOfWords.length <= 1000;
 }

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net, Javascript
  1. January 3, 2013 at 6:43 pm

    Thanks, your solution works to me.

  2. yasserzaid
    January 8, 2013 at 8:14 am

    @Raoni : You are welcome … Good Luck

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: