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

Optimization required for character count in <textarea>

P: n/a
Hey all,
The following function counts for number of characters in a text area
and displays
error msg when it exceeds a maximum limit
I would like to optimize this function so that its highly portable
and elegant
if anybody could do. It will be of great help. Also I would like to add
mincount
ie: a text less than mincount should raise error
----------------------
Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="Javascript">
function checkchars(txtArea,maxLength)
{
txtArea.value = txtArea.value.slice(0, maxLength);
if(txtArea.value.length>=maxLength)
{
alert('Only 20 Characters are Allowed');
}

//Display Remaining characters can type in TextBox
document.getElementById('txtDisplay').value =maxLength -
txtArea.value.length
}
</script>
<TITLE> New Document </TITLE>

</HEAD>

<BODY>
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,20)"></TEXTAREA>
No of Characters Allowed: <INPUT TYPE="TEXT" name="txtDisplay"
id="txtDisplay" value=20 MaxLength=3 disabled=disabled
style="width:25px;border=0" > <BR>
</BODY>
</HTML>

Jan 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
pr**********@gmail.com wrote:
Hey all,
The following function counts for number of characters in a text area
and displays
error msg when it exceeds a maximum limit
I would like to optimize this function so that its highly portable
and elegant
if anybody could do. It will be of great help. Also I would like to add
mincount
ie: a text less than mincount should raise error
----------------------
Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="Javascript">
function checkchars(txtArea,maxLength)
{
txtArea.value = txtArea.value.slice(0, maxLength);

The line above is unneeded and destructive...

Why notthe following?
function checkchars(txtArea,maxLength){
if(txtArea.value.length>=maxLength){
alert('Only '+maxLength+' characters are allowed.');
}
}
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,this.form.txtDisplay.valu e)"></TEXTAREA>

Mick
if(txtArea.value.length>=maxLength) {
alert('Only 20 Characters are Allowed');
}

//Display Remaining characters can type in TextBox
document.getElementById('txtDisplay').value =maxLength -
txtArea.value.length
}
</script>
<TITLE> New Document </TITLE>

</HEAD>

<BODY>
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,20)"></TEXTAREA>
No of Characters Allowed: <INPUT TYPE="TEXT" name="txtDisplay"
id="txtDisplay" value=20 MaxLength=3 disabled=disabled
style="width:25px;border=0" > <BR>
</BODY>
</HTML>

Jan 30 '06 #2

P: n/a
mick white wrote:
pr**********@gmail.com wrote:
Hey all,
The following function counts for number of characters in a text area
and displays
error msg when it exceeds a maximum limit

txtArea.value = txtArea.value.slice(0, maxLength);

The line above is unneeded and destructive...

Why notthe following?
function checkchars(txtArea,maxLength){
if(txtArea.value.length>=maxLength){
/* if(txtArea.value.length>maxLength) */

Mick alert('Only '+maxLength+' characters are allowed.');
}
}
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,this.form.txtDisplay.valu e)"></TEXTAREA>

Mick

Jan 30 '06 #3

P: n/a
What does this piece do ?
-----------------------------------
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,this.form.txtDisplay.valu e)"></TEXTAREA>
-------------
I guess checkchars() uses the value from this textbox
Is that right ?

Jan 30 '06 #4

P: n/a
pr**********@gmail.com wrote:
What does this piece do ?
-----------------------------------
<TEXTAREA name="txtAreaResume" rows="5" wrap="VIRTUAL" cols="40"
onkeyup="checkchars(this,this.form.txtDisplay.valu e)"></TEXTAREA>
-------------
I guess checkchars() uses the value from this textbox
Is that right ?


It passes the value of "txtDisplay" to the checkchars() function, this
way you don't have to hard code the "maximum".

Mick

Jan 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.