SEP 11 2012

Set max length of MultiLine TextBox in ASP.Net

ASP.Net

There was a requirement in a project where I had to set the max length of TextBox in Multiline mode so I just set the MaxLength property of TextBox to the length which was required but it doesn’t worked. So as usual I took help of search engines and found that MaxLength does not work in MultiLine mode and found two alternatives to fix the issue, one is through javascript and another through RegularExpressionValidator.

Here I am going to share the two ways which I found

  • Through Javascript:


    Here is the Javascript code which should be added.

    <script type="text/javascript">
            function Check(textBox, maxLength) {
                if (textBox.value.length > maxLength) {
                    alert("Max characters allowed are " + maxLength);
                    textBox.value = textBox.value.substr(0, maxLength);
                }
            }        
    </script>
    

    Now call the javascript function on two client side event of TextBox namely onKeyUp and onChange like this

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onKeyUp="javascript:Check(this, 100);"
            onChange="javascript:Check(this, 100);" />
    
  • Through RegularExpressionValidator


    Use "^[\s\S]{0,100}$" as Validation Expression to validate through RegularExpressionValidator like this

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="Exceeding 100 characters" ValidationExpression="^[\s\S]{0,10}$" />
    

    You can change the value to in braces according to the requirement. The first value in braces is minimum required length and second value is maximum required length of Multiline TextBox.

Here is all which I found. If you get any other solution then do add your solution in your comment and help others.

All suggestions are welcome.

  • Arun Kumar Singh on said:

    Very nice article. It helped me lot. Thanks for sharing with us. Keep it up.

  • design on said:

    Excellent post. Highly recommended.

  • Kerrie on said:

    Incredible! This blog looks exactly like my old one!
    It’s on a completely different topic but it has pretty much the same layout and design. Wonderful choice of colors!

  • helpful resources on said:

    I couldn’t resist commenting. Perfectly written!

  • Get more info on said:

    Hey there, I think your website might be having browser compatibility issues.
    When I look at your website in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, awesome blog!

  • Uta on said:

    I’m curious to find out what blog system you have been utilizing? I’m having some
    minor security problems with my latest blog and I’d like to find something more safeguarded. Do you have any suggestions?

  • next-sport.info on said:

    This particular post, “Set max length of MultiLine TextBox in ASP.
    Net | Haider M Rizvi” reveals the fact that you really fully understand precisely what u r talking about!
    I totally am in agreement. With thanks -Shanon

  • Scotty on said:

    My spouse and I stumbled over here from a different
    web address and thought I should check things out.
    I like what I see so now i’m following you. Look forward to going over your web page for a second time.

  • Kenton on said:

    wonderful publish, very informative. I wonder why the other experts of this sector do not realize this.
    You should continue your writing. I’m sure, you have a huge readers’ base already!

  • Wendell on said:

    Thank you for the auspicious writeup.

    Look complicated to more brought agreeable from you! By the
    way, how could we keep in touch?

  • jerseys on said:

    Oh my goodness! a tremendous article dude.

  • Lynna on said:

    Oh my goodness! Impressive article dude! Many thanks, However I am experiencing troubles with your RSS. I don’t understand why I am unable to subscribe to it. Is there anyone else having the same RSS issues? Anyone that knows the answer will you kindly respond? Thanks!!

  • Louis on said:

    Howdy! This blog post could not be written much better!
    Looking through this post reminds me of my previous roommate!
    He always kept talking about this. I am going to send this post to him.

    Pretty sure he’s going to have a great read. Many thanks for sharing!

  • Thans on said:

    I think there might be considered a few duplicates, but an exceedingly handy record! I have tweeted this. Numerous thanks for sharing!…

  • jayant on said:

    perfect!!!

  • Charmain on said:

    I’m truly enjoying the design and layout of your website. It’s a very easy
    on the eyes which makes it much more enjoyable for me to come here and visit more often.
    Did you hire out a designer to create your theme? Exceptional work!

  • Dante Arroyo on said:

    I’m very pleased to uncover this site. I want to to thank you for your time just for this fantastic read!! I definitely liked every little bit of it and I have you book marked to look at new stuff on your web site.

  • Warner on said:

    Hi there I am so thrilled I found your site, I really found you by accident, while I was browsing on Google for something else, Regardless I
    am here now and would just like to say thank you for a
    tremendous post and a all round thrilling blog (I also love the theme/design), I don’t
    have time to look over it all at the minute but
    I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep
    up the superb work.

  • Kaylene on said:

    Because the admin of this site is working, no question very
    soon it will be well-known, due to its quality contents.

  • Jasmin on said:

    Hi there! Do you use Twitter? I’d like to follow you if that would be okay.