Javascript to limit number of characters in a input box

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am using the following javascript stored in a single file:
Code:
function limitText(limitField, limitCount) {
    var limitNum = "620";
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

I have an HTML textarea, called txtHistoryComment. The user should only be able to insert 620 characters into this textarea.

I reference the javasript file using the following code in the <head> tag of my page:
Code:
<script language="javascript" type="text/javascript" src="Scripts\jScripts.js"> 
</script>

I also have an input control called "count" for displaying the number of characters remaining.
Code:
<input id="count" name="count" style="font-size: xx-small; width: 21px; font-family: tahoma" type="text" value="620" />

My textarea is declared as following:
Code:
<textarea id="txtHistoryComment" name="txtHistoryComment" onkeyup = "limitText(this.form.txtHistoryComment,this.form.count);" style="width: 282px; font-size: xx-small; font-family: tahoma; height: 88px;" language="javascript" onclick="return txtHistoryComment_onclick()" runat="server" enableviewstate="true"></textarea>

Now I am calling the exact same code in a different page, and it works, however in this case the following errors occur:
1. When you click on the textarea the following javascript error appears - "Error: Object expected."
2. When you start to type in the textarea the following javascript error appears - "Error: 'value' is null or not an object"

Any suggestions on what is causing this error.

Mike55.
 
The cause of the problem is the "runat=server", however this is needed as I have a required field validator attached to this field.

Mike55.
 
Back
Top