By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

Controlling no of lines for text area input

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

<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

P: n/a
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

P: n/a
Thank you

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

This discussion thread is closed

Replies have been disabled for this discussion.