Restricting multiple decimals

I’ve spent a while searching how to restrict more than one decimal point in AS3 but am still momentarily stuck with this…

My most recent attempts have centered around this:

this.input1.addEventListener(KeyboardEvent.KEY_UP,input_key);
function input_key(KeyboardEvent):void{
	
	update_input();
	
}

function update_input() {

	input_1 = input1.text;

	if(input_1.match(".") == false) {
		input1.restrict = ".0-9";
	} else {
		input1.restrict = "0-9";
	}
}

If anyone can help to clear this up I don’t think that I would be the only grateful one!

_thanks all

Try this…


function checkBeforeAdding(evt:TextEvent):void {
	if(evt.text == "." && evt.target.text.indexOf(".") != -1) {
		evt.preventDefault();
	} else {
		// continue as usual
	}
}

input1.restrict = ".0-9";
input1.addEventListener(TextEvent.TEXT_INPUT, checkBeforeAdding);

thanks for the response - this didn’t work out for me. If you are able to check that it works perhaps we would know for sure…

Regarding the original code I posted I can’t figure out why the match function doesn’t work with the decimal - it seems to work with every other character. Perhaps it thinks it is a full stop (period) and therefore doesn’t recognise it.

Any more suggestions anyone at all?

_thanks all

thanks for the response - this didn’t work out for me. If you are able to check that it works perhaps we would know for sure…

Yes. It has been tested and works from my end.

< quite right, good move - works flawlessly> thanks…

is there a similarly slick method of restricting users to entering only to 2 decimal points?

_thanks again

only to 2 decimal points?

You could also try using Regular Expressions…


var decimalPointsLimit:Number = 2;

function checkBeforeAdding(evt:TextEvent):void {
	
	var matches:Array = input1.text.match(/\\./g);
	
	if(evt.text == "." && matches.length >= decimalPointsLimit) {
		evt.preventDefault();
	} else {
		// continue as usual
	}
}

input1.restrict = ".0-9";
input1.addEventListener(TextEvent.TEXT_INPUT, checkBeforeAdding);

Thanks again zig… not working for me though > I’m pretty sure though there is a problem with this line:

var matches:Array = input1.text.match(/\\./g);

Appreciating the help!

_thanks

That’s weird since it works from my end.

Basically it should prevent more than two decimal points (“.”) from entering the input textfield.

Are you getting any kind of errors?

Thanks…no errors… it doesn’t have any effect - to make sure of no typos here’s what I have:

var decimalPointsLimit:Number = 2;

function checkBeforeAdding(evt:TextEvent):void {
	
	var matches:Array = input1.text.match(/\\./g);
	
	if(evt.text == "." && matches.length >= decimalPointsLimit) {
		evt.preventDefault();
	} else {
		// continue as usual
	}
}

input1.restrict = ".0-9";
input1.addEventListener(TextEvent.TEXT_INPUT, checkBeforeAdding);

…this one definitely does seem a bit weird if this code is working for you…

_thanks

That’s the code I’ve used.

Have a look at the attachment.

Thanks… couldn’t see it for some reason - I have CS3 was that 4 by any chance? it said unexpected file format…

Resaved to CS3.

Thanks for that - what I meant actually was one decimal point to be allowed as it was originally and up to but no more than two numbers after, e.g. 121.45

…to be used for currencies…

_thanks

I see what you mean now.

Originally I thought it was “decimal points” rather than “decimal places”.

Try this then…


function checkBeforeAdding(evt:TextEvent):void {
   
    var matches:Array = input1.text.match(/\\./g);
   
    if(evt.text == "." && matches.length >= 1) {
        evt.preventDefault();
	} else if(matches.length == 1 && (input1.text.lastIndexOf(".") + 2 < input1.text.length)) {
        evt.preventDefault();		
    } else {
        // continue as usual
    }
}
 
input1.restrict = ".0-9";
input1.addEventListener(TextEvent.TEXT_INPUT, checkBeforeAdding);

… that’s beautiful zig… not sure why I couldn’t figure that out…

_thanks so much