473,513 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Limit textarea alert message


I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything. Please
advise.
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert("You are trying to enter more
than 500 characters");
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
--------------------------------------------------------------------------------

<form name="myform">
<textarea name="limitedtextarea"
onKeyDown="limitText(this.form.limitedtextarea,thi s.form.countdown,
500);"
onKeyUp="limitText(this.form.limitedtextarea,this. form.countdown,
500);">
</textarea>
Jun 27 '08 #1
3 4176
"te****@hotmail.com" <te****@hotmail.comwrites:
I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything.
Do you get any error messages from the browser?

Does the line previous to the alert work? I.e., is the textarea
contents restricted to 500 chars?
alert("You are trying to enter more
than 500 characters");
The linebreak is not allowed in a string. If this is your actual code,
that could be the problem.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #2
te****@hotmail.com wrote on 07 mei 2008 in comp.lang.javascript:
>
I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything. Please
advise.
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert("You are trying to enter more
than 500 characters");
beware of linebraks!!!
} else {
the else is not needed.
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
-----------------------------------------------------------------------
---------

<form name="myform">
<textarea name="limitedtextarea"
onKeyDown="limitText(this.form.limitedtextarea,thi s.form.countdown,
500);"
onKeyDown="limitText(this,this.form.countdown, 500);"

does the same.
[If you are John,
saying "the son of my father named John" is in effect
the same as saying "me".]

The onkeyup in fact is all that is needed.
onKeyUp="limitText(this.form.limitedtextarea,this. form.countdown,
500);">
</textarea>
This works fine:

==================
<script type='text/javascript'>
var max = 500; // or debug with 5
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
};
</script>

<form name='myform'>
<textarea name='limitedtextarea'
onKeyUp='limitText(this,this.form.countdown,max);' >
</textarea>
<br><br><input name='countdown' readonly>
</form>
===================

Well, not perfect, if you add letters in the middle, another letter will
be deleted, the one at the end.

And not perfect, because you can add a long sentence by pasting [ctrl-V]

So additional tests have to be done, preferably onsubmit [and also
serverside testing, if Javascript is switched off, or if the clientside
code is manipulated.]

Depending on the OS, a <returnwill count for two, or one character.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #3
Evertjan. wrote on 07 mei 2008 in comp.lang.javascript:
This works fine:

==================
<script type='text/javascript'>
var max = 500; // or debug with 5
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
};
</script>

<form name='myform'>
<textarea name='limitedtextarea'
onKeyUp='limitText(this,this.form.countdown,max);' >
</textarea>
<br><br><input name='countdown' readonly>
</form>
===================

Well, not perfect, if you add letters in the middle, another letter
will be deleted, the one at the end.

And not perfect, because you can add a long sentence by pasting
[ctrl-V]

So additional tests have to be done, preferably onsubmit [and also
serverside testing, if Javascript is switched off, or if the
clientside code is manipulated.]
Try this, IE tested, the oncontextmenu seems IE specific:

================
<script type='text/javascript'>
var max =10;
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length limitNum) {
limitField.value = limitField.old;
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
limitField.old = limitField.value;
};
</script>

<form name = 'myform'>
<textarea name = 'limitedtextarea'
onkeydown = 'this.old=this.value;'
onKeyUp = 'limitText(this,this.form.countdown,max);'
oncontextmenu = 'alert("No pasting please");return false;'>
</textarea>
<br><br><input name='countdown' readonly>
</form>
================

Ctrl-V is only allowed if the result is within limit.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5895
by: iceking | last post by:
Hi, I have a php script that checks whether the user is allowed to perform an action. If he is not allowed; I display a warning; using the alert function. After clicking away this function, I want...
1
1385
by: Vivian | last post by:
I've created a web page with ASP .NET and C#. Say my aspx filename is "WebForm1.aspx", I have created another C# file named "Details.cs". My problem is I want to prompt an alert message from both...
22
2291
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. Using...
3
3940
by: Wayne Deleersnyder | last post by:
Hi All, I'm trying to create a function that will cause a pop-up alert to appear if dates which were chosen from a drop-down list were invalid on a page. There's 4 dates, so there's the...
15
7309
by: cyberlei | last post by:
Hey guys, i was trying to use alert message for php string,but dunno why the alert can`t be displayed, so please help me where i did wrong, THANKS SO MUCH here is the code: <SCRIPT...
5
3980
by: cyberlei | last post by:
Hey Guys, Just wandering if I can display alert message with a default sound. the current code is below and the problem is no sounds. function disp_alert() { alert("test") } and...
3
9613
by: nagmvs | last post by:
Hai guys, I want the the code to disable Left Mouse button with out Alert message. Below code i am having <!-- This code is for disableing left click--> <script language="javascript">...
2
2182
by: shrinivas singh | last post by:
If I remove alert messsage,it works fine.But, with alert message nothing is displayed after alert message but the blank screen.Below is the code: <iframe width = "100%" height = "1100" src =...
0
2377
by: Anupam Jana | last post by:
Hello All, I want to redirect to some other directory page after displaying JavaScript alert message in ASP.net in a page. For that I tried bellow code in our application...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
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
7391
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
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
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...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.