Click here to Skip to main content
15,884,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am restricting the user to enter a max limit(40Char) of characters in a text area.
Calling this below JS function on onKeyUp event of textarea.
But when i copy paste a content which is more than 40chars, not gettinng pop-up(Alert) until i do a keyup/down on the textarea.

Is there a way to get alert without keyup/down after pasting into textarea?
Thanks in advance!


C#
function checkMaxLength(textBox, e, maxLength) {
    if (textBox.value.length > maxLength - 1) {        
        alert("* You have reached max limit ");
        textBox.value = textBox.value.substr(0, maxLength);
    } else {
        alert(maxLength - textBox.value.length + " characters remaining");
    }



I have tried the below approach, It is working fine on IE,Safari,Chrome but not working on Mozilla Firefox, Any thoughts on this?

HTML
<HTML><head></head><body>
<form name="myForm">
Text: <input type="text" name="myText"
 önKeyUp = "fncKeyLength(this);">
</form>
<script>
function fncKeyLength(text){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
   alert("<br />The string is this long: " + text.length);
 
}
}}
</script></body></HTML>
Posted
Updated 30-Jul-12 19:38pm
v3

1 solution

check this url

copy paste[^]
 
Share this answer
 
Comments
Donthi Thirupathi Reddy 31-Jul-12 1:36am    
I have tried the below approach, It is working fine on IE,Safari,Chrome but not working on Mozilla Firefox, Any thoughts on this?

<HTML><head></head><body>
<form name="myForm">
Text: <input type="text" name="myText"
önKeyUp = "fncKeyLength(this);">
</form>
<script>
function fncKeyLength(text){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
alert("<br />The string is this long: " + text.length);

}
}}
</script></body></HTML>
Shemeer NS 31-Jul-12 9:20am    
In mozilla "window.event.keyCode" won't work.. add this line var code = (event.keyCode) ? event.keyCode : event.which; and use variable code to compare the key...
Donthi Thirupathi Reddy 31-Jul-12 9:24am    
Thank you! It really helped me.
It is working fine for ctrl+v, but not if we do mouse right click + paste.
Any help on this?
Shemeer NS 31-Jul-12 11:15am    
try https://developer.mozilla.org/en/DOM/element.onpaste

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900