Optimization required for character count in <textarea>

P

princeofcode

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>
 
M

mick white

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.value)"></TEXTAREA>

Mick


if(txtArea.value.length>=maxLength)
 
M

mick white

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top