views:

210

answers:

2

I want to cancel out key press event when my textarea field gets n number of characters from user. At that point i dont want user to enter any more characters. What to do?

Edit:

I need it to be done in javascript as well as in php

A: 

In Winforms, you would set Handled in the KeyPressEventArgs to true to cancel the KeyPress event. This keeps the control from processing the key press.

Note: Some controls will process certain key strokes on KeyDown. For example, RichTextBox processes the Enter key before KeyPress is called. In such cases, you cannot cancel the KeyPress event, and must cancel the key stroke from KeyDown instead.

http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx

Robert Harvey
@Robert: I could have accepted your answer, but Question was not the one you answered for. I was trying to cancel javascript event.
Shantanu Gupta
OK, but your comment said you wanted it for Winforms also.
Robert Harvey
+1  A: 

In web pages, NobleCount is one possibility. Seems to be the most robust of the jQuery plugin solutions. It will give your user a count of the number of characters remaining, Twitter-style.

http://tpgblog.com/noblecount/

See also:

Interactive character limit for textarea using Jquery http://www.ajaxray.com/blog/2007/11/09/interactive-character-limit-for-textarea-using-jquery/

Robert Harvey