Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
__TR__ 7-Jan-13 12:35pm    
ridoy 7-Jan-13 14:04pm    
What is the error message?
Sergey Alexandrovich Kryukov 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

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[^]
 
Share this answer
 
Here you go. Use the following RegularExpression ^[\s\S]{0,350}$ with RegularExpressionValidator control of Asp.net.

--Amit
 
Share this answer
 
Use This jquery function
XML
<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>
 
Share this answer
 
v2
hi dear,

just try to use your function on keydown event insted of keypress event.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900