473,287 Members | 1,714 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,287 software developers and data experts.

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 4162
"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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.