Skip to main content
Inspiring
July 1, 2009
Question

Character limit on edit field

  • July 1, 2009
  • 2 replies
  • 2371 views

I need to restrict an edit field to only allow 50 characters (Due to database limits etc). I thought it may have been in the "Configuration" class but cant see anythign of the nature in there.

How do I do this?

This topic has been closed for replies.

2 replies

a_circulo
Participating Frequently
October 21, 2009

the problem was on the selection index... so here is the final code I'm using now in case it can be of any help for anyone:

searchFieldTextFlow.addEventListener(UpdateCompleteEvent.UPDATE_COMPLETE, searchUPDATE);

private function searchUPDATE(event:UpdateCompleteEvent):void {

if (event.target.textLength > 10) {

event.target.interactionManager.selectRange(9, event.target.textLength);

event.target.interactionManager.deleteText();           

}else{

string = TextConverter.export(event.textFlow, TextConverter.PLAIN_TEXT_FORMAT, ConversionType.STRING_TYPE).toString().toLowerCase();

/// my search trigger code

        }

}

- I get some erros with setSelection it looks like it is deprecated.. anyway, I just stuck with selectRange

- this is a Textflow I'm using as a search field input

- I have no strict reason to have chosen the UpdateCompleteEvent instead of the FlowOperationEvent, besides that the updateComplete one has the textFlow property on the event, so it came in hand for this search field

A_Shiyaz
Known Participant
August 13, 2010

The methods have changed now and the livedocs are not updated yet. Here is the updated code which would work with TLF build no: 604 (748113) extracted from flex_sdk_4.1.0.16076

This code has the updated SelectionState implementation. Further, the livedocs for IEditManager mentions it just as deletePrevious and deleteNext. It should be deletePreviousCharacter and deleteNextCharacter.

_textFlow.addEventListener(UpdateCompleteEvent.UPDATE_COMPLETE, function(e):void { restrictMaxChar(e, _textFlow, maxLen) } );

private function restrictMaxChar(e:Event, textFlow, maxLen):void {
    trace("> ", textFlow.textLength-1, maxLen);
    if (textFlow.textLength-1 > maxLen) {
        var selState = new SelectionState(textFlow, textFlow.interactionManager.activePosition, textFlow.interactionManager.activePosition);
        (textFlow.interactionManager as IEditManager).deletePreviousCharacter(selState);
    }
}

Thanks to all the previous contributors.

~ Shiyaz

Adobe Employee
July 1, 2009

Take a look at the flex gumbo code.  I believe they've done this.

Basically it requires listening for flowOperatinBegin events on the TextFlow and modifying or cancelling operations that exceed the limit.

Hope that helps,

Richard

Participant
July 1, 2009

Dont suppose you have a link to the example. I dont use flex, heve flex and probably never will. I'm not sure where to find such a resourse.

I figured out how to attach a listener to the textFlow like this.

function flowOpListen(e:FlowOperationEvent)
{
  trace(e.operation);
}
textFlow.addEventListener(FlowOperationEvent.FLOW_OPERATION_BEGIN ,flowOpListen);

But cant figure out any more than that.

Adobe Employee
July 2, 2009

The operations are listed here:

http://livedocs.adobe.com/flex/gumbo/langref/flashx/textLayout/operations/package-detail.html

There's a couple of ways to proceed.

1) cancel any PasteOperation or InsertTextOperation that would give you more than 50 characters.  You can do that by calling preventDefault on the flowOperationBegin event.  The other operations aren't accessible via the keyboad

2) modify any operation that would give you more than 50 characters to restrict it

See the other post regarding examining the gumbo code.

Regards,

Richard