This example shows how to set the text max length in the ASPxMemo control.
If an end-user types in text or pastes it by using a corresponding shortcut, you can crop the text in the KeyDown event handler when its length exceeds the max value. If the end-user pastes text to ASPxMemo by using the context menu, you can crop the text in the “paste” event handler of the ASPxMemo “textarea” element. We disable the context menu for ASPxMemo in the Opera browser because it doesn’t provide the “paste” event.
See also:
ASPxMemo - How to limit the length of text that can be entered into the control using MaxLength
How to implement the "characters remaining" functionality for ASPxTextBox and ASPxMemo using MaxLength
Question Comments
Added By: James Parker at: 11/16/2012 7:43:59 AM
ClientInstanceName is missing from the ASPxMemo in the sample code. It should be:
<dx:ASPxMemo ID="memo" runat="server" Height="71px" Width="170px" ClientInstanceName="memo">
<ClientSideEvents Init="memo_OnInit" KeyDown="memo_OnKeyDown" />
Besides lots of errors on this script, it will allow to type lowercase 'z' and numbers with the Numeric Pad even if maxLength has already been reached!!
I have changed it to somgething like:
function memo_OnKeyDown(s, e) {
if (s.GetText().length >= se.GetNumber())
CorrectTextWithDelay();
// Ctrl + V
if (e.htmlEvent.keyCode === 86 && e.htmlEvent.ctrlKey)
CorrectTextWithDelay();
}
I've changed the script so the maxLength value does not get taken from the SpinEdit control, but from a variable. I've also make use of the s parameter to target the sender instead of using the ClientInstanceName value:
var memoMaxLength = 1000;
function memo_OnKeyDown(s, e) {
if (s.GetText().length >= memoMaxLength)
CorrectTextWithDelay(s);
// Ctrl + V
if (e.htmlEvent.keyCode === 86 && e.htmlEvent.ctrlKey)
CorrectTextWithDelay(s);
}
function memo_OnInit(s, e) {
var input = s.GetInputElement();
if (ASPxClientUtils.opera)
input.oncontextmenu = function () { return false; };
else
input.onpaste = function () { CorrectTextWithDelay(s); };
}
function CorrectTextWithDelay(s) {
setTimeout(function () { s.SetText(s.GetText().substr(0, memoMaxLength)); }, 0);
}