473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2185
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt | last post by:
Simple vbscript,html page loading into access. Input pg posting to Confirm pg, then sending to access. when using; <textarea name="name" tabindex="15"> </textarea> as an input renders tab...
1
by: Augustus | last post by:
Hiya, I have a form with a <textarea></textarea> to receive user input. This input is then stored in a database and sent by fax... I need to be able to remove the carriage returns (enter...
4
by: Dennis Allen | last post by:
Hi. I hope someone here can help. I'm webmaster for a local astronomy club. Just went over our web site. Have validated every htm file on the site except:...
3
by: Bart Van der Donck | last post by:
Hello, I have a dynamic page of which I don't know how many forms will be on it, neither which and how many elements will be in each form. I use the following java script to disable all...
2
by: Jonathan Taub | last post by:
This may seem a stupid question. I've got a <textarea> element: .... <td> List of items: <textarea> 1. Apple 2. Orange 3. Box
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
3
by: Jarek Mielcarek | last post by:
hi all, in xml file I have some fields which are source for <textarea> element. I'd like to transform this file using xslt and set the rows property of <textarea> depend of lines in some source...
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
3
by: FunkHouse9 | last post by:
I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.