How to remove carriage return char after it's pressed?

I have a textarea which is restricted to input only numbers. I want to remove carriage return char after user presses enter key. Here's my code:

// Change tempo
function changeTempo(event:KeyboardEvent):void {
    if (event.charCode == 13) {
        // Some code here
    }
    // Remove enter char
    removeCarriageReturnsAndNewLines(tempo_txt.text);
}

function removeCarriageReturnsAndNewLines($myString:String):String {
    var newString:String;
    var findCarriageReturnRegExp:RegExp = new RegExp("\r", "gi");
    newString = $myString.replace(findCarriageReturnRegExp, "");
    var findNewLineRegExp:RegExp = new RegExp("\n", "gi");
    newString = newString.replace(findNewLineRegExp, "");
    return newString;
}

Answers


I would say the easiest way is to listen to text input, something like this:

var t:TextArea = this.ta; //ta is on the timeline
t.restrict = "0-9"; //restricts the input only to numbers
t.addEventListener(TextEvent.TEXT_INPUT, onTextInput, true); //use capture phase to be able to prevent the default behavior (text input)

function onTextInput(e:TextEvent):void {
    if(e.text.indexOf("\n") > -1) {
        e.preventDefault(); //prevent the default behavior of the field
    }
}

I cannot test this right now but I guess it should work without problems.


Need Your Help

Acts like an array of structs without declaring array

c++ arrays class struct output

I started to write this code for fun and I was surprised that it compiled and worked. (I'm using Dev C++ to compile.) I wanted to create an array of structs but before I created the array, It store...

How does ng-repeat work?

angularjs angularjs-ng-repeat

I dissected ng-repeat and extracted the code blocks attached, seeing that these comprise the logic that handles the repeating algorithm (which I want to understand how it works).