Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I have a textbox with multiline enabled . I want it to accept only 350 characters so i used max length property and it throws me error message too, but the prob is its accepting more than 350.I am new to java script so help me with scripts.
Posted 7-Jan-13 6:29am
Comments
__TR__ at 7-Jan-13 12:35pm
   
ridoy at 7-Jan-13 14:04pm
   
What is the error message?
Sergey Alexandrovich Kryukov at 7-Jan-13 18:33pm
   
First of all, there is no such thing as "Java script". Java is not a scripting language. :-)
And JavaScript has nothing to do with Java, if you never knew that. "Java Script" in nothing but gibberish.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is the simple solution using javascript. it also validate data length in case of Copy/Paste/.
 
http://developer.sheikhharis.com/2010/06/max-length-not-working-for-multi-line.html[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here you go. Use the following RegularExpression ^[\s\S]{0,350}$ with RegularExpressionValidator control of Asp.net.
 
--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use This jquery function
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
 
    $(document).ready(function () {
        $("#MainContent_txtFirst").keypress(function (e) {
            var strr = document.getElementById("MainContent_txtFirst").value;
            //var strrlen = strr.length;

            if (strr.length > 349 && e.keyCode != 46 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 39 && e.keyCode != 37 && e.keyCode != 8) {
                alert("Only 350 characters are allowed");
                return false;
            }
            });
    });
 
</script>
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi dear,
 
just try to use your function on keydown event insted of keypress event.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100