Forum Moderators: open
if msg crosses max limit alert will come. and if i press entre due to firefox event model it is captured in textarea and problem starts
var ctrl = e['ctrlKey'];
//alert(ctrl)
if((typeof e.keyCode!= "undefined")&&(e.keyCode == 13))
{//alert(1)
entr = true;
}
else
{//alert(2)
entr = (e.charCode == 13);
}
Could you try something like
if((typeof e.keyCode!= "undefined")&&(e.keyCode == 13))
{//alert(1)
entr = true;
}
else
{//alert(2)
entr = false;
}
this would maybe then stop that enter (chr(13)) from being fired.
var ctrl = e['ctrlKey'];
It's entirely possible Firefox is not seeing this? I recall the Moz's have a different way of reading keystrokes.
I was just doing this yesterday. It's got some picadillos (like if someone pastes a huge block in it will keep hammering them with messages every keyUp) but for direct entry it seems to work OK. Maybe it will get you thinking in a different direction:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
// You could pass any string to it from anywhere, really
function checkLen(str,len) {
if (str.length > len) { alert('The message field is only allowed 255 characters.'); return 0; }
}
// just added for this example
function chkForm(form) {
var txt = form.message.value;
var err = checkLen(txt,255);
if (err!= 0) { alert(err); form.submit(); }
}
</script>
</head>
<body>
<form method="post" action="whatever.pl" onSubmit="return false;">
<textarea name="message" rows="3" cols="45" onKeyUp="checkLen(this.value,255);"></textarea><br>
<input type="submit" onClick="chkForm(this.form);" value="submit">
</form>
</body>
</html>
which checks for the length as each char is entered, and will pop up a message when you reach the limit, and crop whatever is too long..
i realise you seem to want to be able to let the user press enter on the textarea to submit it..?
which might be confusing for the average schmo, as i dont think its the default behaviour..
anyhoo, i think maybe you need to add, as i have on keyup and down.. dunno
function textAreaCounter(t){
var textLength = t.value.length;
var counterInput = document.getElementById("counter");
if(textLength > 500){
alert("Sorry, the maximum length of comments is 500 characters");
t.value=t.value.substring(0,500);
}
counterInput.value = (500-parseInt(t.value.length));
}
not saying its perfect. but as far as i know it works for IE,FF,safari ..
- the counterInput is just a little text field that lets you know how may chars you have left as you type..
hth