STEK
STEK

Reputation: 35

Remove commas from input text in as3

Can anyone tell me how to implement, a split maybe? To remove commas from user input text numbers before running some if statements. I know I can restrict commas, but I'd like people to be able to use commas when imputing values. Thanks!

value_enter.addEventListener(MouseEvent.CLICK, release1);
function release1(evt:MouseEvent):void 
{
if ( Number(inputfield.text) >0 &&  Number(inputfield.text) <2600)
{
gotoAndStop(2);
}
else if ( Number(inputfield.text) >2599 &&  Number(inputfield.text) <4200)
{
gotoAndStop(3);
}

Upvotes: 2

Views: 2081

Answers (3)

strah
strah

Reputation: 6732

Try this:

value_enter.addEventListener(MouseEvent.CLICK, release1);
function release1(evt:MouseEvent):void 
{
    var strNum:String = String(inputfield.text).replace(/,/g, "");
    var num:Number = Number(strNum);

    if ( num > 0 &&  num <2600)
    {
        gotoAndStop(2);
    }
    else if ( num >2599 &&  num <4200)
    {
        gotoAndStop(3);
    }
}

Upvotes: 3

ToddBFisher
ToddBFisher

Reputation: 11600

If you wanted to remove all instances you can do a split like you were thinking:

if your inputfield.text = "123,456,789";

then:

var inputNoCommas:String = inputfield.text.split(",").join("");

should return `"123456789";

As I recall String.replace() only replaces the first instance, which if that is all you want then you can go that route.

Upvotes: 0

laurent
laurent

Reputation: 90794

Just use the replace() function:

inputfield.text = inputfield.text.replace(",", "");

Upvotes: 3

Related Questions