469,331 Members | 4,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Optimization required for character count in <textarea>

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
4 2021
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
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
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
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.

Similar topics

5 posts views Thread by Matt | last post: by
4 posts views Thread by Dennis Allen | last post: by
3 posts views Thread by Bart Van der Donck | last post: by
2 posts views Thread by Jonathan Taub | last post: by
11 posts views Thread by Les Paul | last post: by
8 posts views Thread by ASP Yaboh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.