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