Solved

ext.net - TextArea limit word in textarea to 10 - max words

Posted on 2013-02-01
2
850 Views
Last Modified: 2013-02-03
Ext.net ver 2.0

I have a 5 textareas defined and I want to the stop the user from entering more that 10 words in each textarea. Should I be using listners and what js code should I implement. I am weak in Js. Thanks for your help.
 
<ext:TextArea id="id1" runat="server" enableKeyEvents="true">
<ext:TextArea id="id2" runat="server" enableKeyEvents="true">
<ext:TextArea id="id3" runat="server" enableKeyEvents="true">
<ext:TextArea id="id4" runat="server" enableKeyEvents="true">
<ext:TextArea id="id5" runat="server" enableKeyEvents="true">
0
Comment
Question by:DianaGee
2 Comments
 
LVL 10

Expert Comment

by:Monica P
ID: 38846521
The onchange will prevent context-menu pasting, and the onkeyup will fire for every keystroke


for example

<script type="text/javascript">
function change (el) {
var max_len = 10;
if (el.value.length > max_len) {
el.value = el.value.substr(0, max_len);
}
document.getElementById('char_cnt').innerHTML = el.value.length;
document.getElementById('chars_left').innerHTML = max_len - el.value.length;
return true;
}
</script>

Open in new window


<form>
<textarea cols=100 rows=20 onkeyup="change(this);"></textarea>
<br>You've typed <span id="char_cnt">0</span> character(s). You
are allowed <span id="chars_left">lots</span> more.
</form>

Open in new window


to check it for more

http://snipplr.com/view/13601/
http://stackoverflow.com/questions/1125482/how-to-impose-maxlength-on-textarea-in-html-using-javascript/1125521#1125521
0
 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
ID: 38849271
To have limit on words you can use this: http://jsfiddle.net/4QZy2/2/

<textarea name="description" id="description" onkeypress="return validateText(this);"></textarea>

Open in new window


with the following javascript code:

function validateText (el) {
    var maxWordLength = 10;
    var array = el.value.split(' ');
    if (array.length <= maxWordLength)
    {
        return true;
    }
    return false;
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question