Controlling no of lines for text area input

E

effendi

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.
 
S

Stephen Chalmers

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.
 
R

RobB

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top