0

I am Using MultiLine Textbox now I want to allow all the characters in it and I also want to set the limit of the textbox to 150 characters. So I want to use the regular expression so any one can help me. I am using this regular expression:

"^[A-Za-z0-9\s!@#$%^&*()_+=-`~\\\]\[{}|';:/.,?><]*{1,150}$"

All working fine but it does not limit the characters to 150

Thanks in advance.

nothingisnecessary
  • 6,099
  • 36
  • 60

3 Answers3

0

You can use TextBox.MaxLength Property if TextMode property not set to MultiLine. MaxLength property does not work for multiline textbox.

Gets or sets the maximum number of characters allowed in the text box. The value you set is the maximum number of characters allowed in the text box. The default is 0, which indicates that the property is not set.

<asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>

If you want to let the user know if he exceeded the amount of characters as he writes, you could use a javascript function attached to keypress event. This function would test the length of the input and cancel the character rendering if the maxlenght was reached.

Check this: Set max length of MultiLine TextBox in ASP.Net

If you want to use the MultiLine mode then use java-script and RegEx way to achieve the goal and follow the below reference links:
You can do it this way:

<asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" MaxLength="10" onkeyDown="return checkTextAreaMaxLength(this,event,'10');"  TextMode="multiLine" CssClass="textArea" runat="server"> </asp:TextBox>

//js

// JScript File

function checkTextAreaMaxLength(textBox, e, length) {

    var mLen = textBox["MaxLength"];
    if (null == mLen)
        mLen = length;

    var maxLength = parseInt(mLen);
    if (!checkSpecialKeys(e)) {
        if (textBox.value.length > maxLength - 1) {
            if (window.event)//IE
            {
                e.returnValue = false;
                return false;
            }
            else//Firefox
                e.preventDefault();
        }
    }
}

function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}     

References:
ASP.NET - Limit number of characters in TextBox control
Limit Number Of Characters In an ASP.NET Multiline TextBox using jQuery
TextBox: Minimum and Maximum Character Length Validation using ASP.Net RegularExpression Validators
Limit the number of characters of a textbox
Specifying maxlength for multiline textbox
How to set maxlength for multiline TextBox?
asp text box limit number of characters?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

It is really easy to do this: Newer browsers support the maxlength attribute on textarea elements (but not IE9 and older versions of Internet Explorer).

You can limit textarea with javascript, or at least get the character count if you want to show "x chars remaining" (like posting a comment in SO).

For older browsers this javascript will check the length against the maxlength and prevent user from typing. However, since users can potentially defeat the keydown approach via copy and paste, etc., it is also a good idea to use keyup to truncate any overflow. Also, if data is going into the server/database it is not a bad idea to truncate and sanitize the input received from client in case the client bypasses the form and uploads POST data directly in which case maxlength has no effect.

In ASP.NET the markup looks like: <asp:TextBox ID="txt" runat="server" TextMode="Multiline" ClientIDMode="Static" maxlength="150"/> but for textarea (TextMode="Multiline") the maxlength attribute may not be rendered (perhaps depending on your ASP.NET version). You can verify in the markup whether it was rendered. If not, you may need to manipulate the TextBox's Attributes in the codebehind like this: txt.Attributes["maxlength"] = "150"; (assuming C#)

JS Fiddle example: http://jsfiddle.net/aymjsfdp/

// "maxlength" code for older browsers including IE9 and previous versions
var txt = document.getElementById('txt');
txt.onkeyup = function () {
    var maxlength = parseInt(this.getAttribute("maxlength")) || 0;
    if (maxlength > 0 && this.value.length > maxlength) {
        this.value = this.value.substring(0, maxlength);
        alert('text was truncated to ' + maxlength + ' characters.');
    }
    document.getElementById('length').innerHTML = this.value.length.toString();
};
txt.onkeydown = function () {
    var maxlength = parseInt(this.getAttribute("maxlength")) || 0;
    if (maxlength > 0 && this.value.length >= maxlength && (event.keyCode || event.which) > 49) {
        return false;
    }
};
TEXTAREA {
  width: 300px;
  height: 60px;
}
<div>Chars: <span id='length'></span>
</div>
<textarea id='txt' maxlength='150'></textarea>
nothingisnecessary
  • 6,099
  • 36
  • 60
-1

Another Way Below Code is available on the link:

http://www.aspsnippets.com/Articles/Limit-number-of-characters-in-an-ASPNet-Multiline-TextBox-using-jQuery.aspx With the Demo Sample

<title>title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">script>

<script type="text/javascript" src="MaxLength.min.js">script>

<script type="text/javascript">

    $(function () {

        //Normal Configuration

        $("[id*=TextBox1]").MaxLength({ MaxLength: 10 });



        //Specifying the Character Count control explicitly

        $("[id*=TextBox2]").MaxLength(

        {

            MaxLength: 15,

            CharacterCountControl: $('#counter')

        });



        //Disable Character Count

        $("[id*=TextBox3]").MaxLength(

        {

            MaxLength: 20,

            DisplayCharacterCount: false

        });

    });

script>

head>

<form id="form1" runat="server">

<div id="counter">

div>

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300" Height="100"

    Text="Mudassar Khan">asp:TextBox>

<br />

<br />

<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="300" Height="100">asp:TextBox>

<br />

<br />

<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Width="300" Height="100">asp:TextBox>

form>

body>

html>