Limiting the Number of Characters in a MultiLine TextBox

Solution 1 : Use JavaScript (This option is not safe since the javascript on the browser can be turned off)

string MaxLimitJavaScript = “javascript:”;
MaxLimitJavaScript += “if (document.getElementById(‘” + txtTEST.ClientID + “‘).value.length > 100)”;
MaxLimitJavaScript += “document.getElementById(‘” + txtTEST.ClientID + “‘).value = document.getElementById (‘” + txtTEST.ClientID + “‘).value.substring(0, 100);”;

txtTEST.Attributes.Add(“onKeyup”, MaxLimitJavaScript);

Solution 2 : Use a Regular Expression Validator

RegularExpressionValidator RExpV= new RegularExpressionValidator();
RExpV.ControlToValidate = txtDescription.ID;
RExpV.Enabled = true;
RExpV.EnableViewState = true;
RExpV.EnableClientScript = true;
RExpV.Display = ValidatorDisplay.Static;
RExpV.Text = ” << Maximum Characters Allowed are 100″;
RExpV.ValidationGroup = “form validation”;
RExpV.ValidationExpression = “^[0-9\na-zA\r-Z,;*^!_+=@#-:.’?\t&%$><{()}/]{1,100″}$”;

Advertisements

~ by Neha Sinha on June 27, 2008.

4 Responses to “Limiting the Number of Characters in a MultiLine TextBox”

  1. Hi Neha,
    There is one more simple option to make this work
    How about using MaxLength property of a Multiline Text Box… you can limit the character size of the Text Box
    This option is available in the Visual Studio…
    I have used in the VB.NET

    Anyways thanks for the Second Method that is Using Regular Expression’s way to handle this problem …
    I learnt one more way to limit the character size of textbox

  2. Its not working for me

  3. Can we connect on this subject?

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: