473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Controlling no of lines for text area input

Is there a way I can control the number of lines a user enters in a
textarea? I like to allow user to enter let say 5 lines and no more
than that.

Thanks in advance.

Jul 23 '05 #1
3 1474

<ef*****@epitome.com.sg> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Is there a way I can control the number of lines a user enters in a
textarea? I like to allow user to enter let say 5 lines and no more
than that.

Thanks in advance.


It's more practical to work in terms of the number of characters rather than
lines.
The .value.length property of the textarea element stores the number of
characters entered, but how you use it to enforce a limit is a matter of
choice. In any case, it's popular practice to maintain a numeric display of
the number of remaining characters.
--
S.C.

Jul 23 '05 #2
ef*****@epitome.com.sg wrote:
Is there a way I can control the number of lines a user enters in a
textarea? I like to allow user to enter let say 5 lines and no more
than that.

Thanks in advance.


Keep in mind, this can be bypassed by c & p. Could check it onsubmit as
well, depends on how pushy you need to be about it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<style type="text/css">

#ta {
width: 400px;
height: 72px;
font: normal 11px arial;
}

</style>
<script type="text/javascript">

function line_limit(e)
{
var tgt, t, kC, n = 5;
e = e || window.event;
if (e
&& (tgt = e.srcElement || e.target)
&& (t = tgt.type)
&& /textarea/.test(t)
&& (kC = e.keyCode || e.which))
{
var m = tgt.value.match(/((\r\n)|\n|\r)/g);
if (!m
|| m.length < n - 1
|| kC != 13)
return true;
else
{
alert(n + ' lines only, dude !');
return false;
}
}
}

window.onload = function(el)
{
if (document.getElementById
&& (el = document.getElementById('ta')))
el.onkeypress = line_limit;
}

</script>
</head>
<body>
<form>
<textarea id="ta" wrap="hard"></textarea>
</form>
</body>
</html>

Replace this:

else
{
alert(n + ' lines only, dude !');
return false;
}

....with

else return false;

....to eliminate the prompt. ;=D

Jul 23 '05 #3
Thank you

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Mark Reginald James | last post by:
Hi, I'm trying to use a keyboard event function to change what element gets the focus after tab is pressed in a particular element. Simple HTML that demonstrates the problem is below. When...
1
by: leo | last post by:
Hello Guys I have this problem in controlling a the text in a Text Area. How can I do that for example the field size is 200, when it reaches 50 it automatically goes to the next line. Bcoz im...
3
by: leo | last post by:
Hello Guys I have this problem in controlling a text in a Text Area. How can I do that, for example the field size is 200, when it reaches 50 it automatically goes to the next line. Because im...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
7
by: Robert | last post by:
I haven't found anything at google sa i post here, hoping someone will help me I have written sth like this: document.form.inputfield.value += 'aaa' document.form.inputfield.focus() these...
4
by: devine | last post by:
Hi All, I am VERY new to Javascript. I have been provided with some code, which will enable me to hide/show a text area and change a submit button dependant on a check box. <!DOCTYPE html...
4
by: high | last post by:
hi, im new to this platform, i have been creating a web page using MS Front page and i need to insert a text area to give the input and another text area to store that, <form...
1
by: bbcline | last post by:
Hi all, I'm a newbie to the forum and a Javascript beginner, I'd appreciate some help with a drop down box issue. Is it possble to limit the options/attributes in a drop down box based on how the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.