Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Contributor
Posts: 8
Registered: ‎12-14-2010
My Device: Not Specified
Accepted Solution

textField.restrict - how to just allow one decimal

Hello,

 

I read somewhere about the textField.restrict.

I am using it to restrict the user input to just numbers and the decimal point.

 

a.textField.restrict = ". 0-9";

 

However, I am finding that the user can input more than one decimal.

 

Does anyone know how to restrict the user input to just one decimal?

 

thanks for the help.

New Developer
Posts: 8
Registered: ‎12-18-2010
My Device: Not Specified

Re: textField.restrict - how to just allow one decimal

Iry setting a.textField.maxChars = 1. It will limit to only one character. 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: textField.restrict - how to just allow one decimal

[ Edited ]

hey mlin13,

 

unfortunately you cannot restrict it so it just allows one of a certain kind of character in that fashion. however a work around would be when the user "focuses out" of the TextInput you can run a function on the TextInput object and reset the value so there is only one decimal. here is a quick and easy way of doing just that:

 

 

private var myInput:TextInput;

myInput = new TextInput();
myInput.setSize(300,50);
myInput.textField.restrict = "0-9 \.";
myInput.addEventListener(FocusEvent.FOCUS_OUT, checkInput);

addChild(myInput);


(...)


private function checkInput(e:FocusEvent):void
{
e.target.text = parseFloat(e.target.text);
}

 

so when a user stops typing and the focus on the textinput object is gone the checkInput function is called and it rewrites the value of the input object's text to a float. hope that help. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Highlighted
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: textField.restrict - how to just allow one decimal

this should work for you

 

 

package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.TextEvent;

public class Test extends Sprite
	{
	public function Test()
		{
		var tf:TextField = new TextField();
		tf.type = TextFieldType.INPUT;
		tf.border = true;
		tf.width = 200;
		tf.height = 16;
		tf.x = tf.y = 20;
		tf.restrict = ".0-9"
		tf.addEventListener(TextEvent.TEXT_INPUT, restrictOneDecimal);
		
		addChild(tf);
		}
		
	function restrictOneDecimal(evt:TextEvent):void
		{
		var matches:Array = evt.currentTarget.text.match(/\./g);
	   
		if	(evt.text == "." && matches.length >= 1)
			evt.preventDefault();
		}
	}
}

 

or if you want to restrict to one decimal as well as a specific number of decimal places:

 

 

function restrictDecimalPlaces(evt:TextEvent):void
	{
	var matches:Array = evt.currentTarget.text.match(/\./g);
	var allowedDecimalPlaces:uint = 2;
   
	if	((evt.text == "." && matches.length >= 1) ||
		(matches.length == 1 && (evt.currentTarget.text.lastIndexOf(".") + allowedDecimalPlaces < evt.currentTarget.text.length)))
		evt.preventDefault();
	}

 

 


PlayBook Applications:
Drop Swatch
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: textField.restrict - how to just allow one decimal

hey,

 

well played darkin! that works on my end too. thanks for the correction Smiley Happy

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: textField.restrict - how to just allow one decimal

i'm pretty sure there's also a way to (much more simply) accomplish this by setting a regular expression as the value of the text field's restrict property.  i never really had such luck doing so, but i'm looking into it.


PlayBook Applications:
Drop Swatch
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: textField.restrict - how to just allow one decimal

 


TheDarkIn1978 wrote:

i'm pretty sure there's also a way to (much more simply) accomplish this by setting a regular expression as the value of the text field's restrict property.  i never really had such luck doing so, but i'm looking into it.


 

Given the documentation of the restrict feature it looks like the entire content of that String is effectively used as a "character set" inside a regular expression that would look something like /^[restrict-set]*$/  (where the entire content of your  "restrict" string would be inside the square brackets in that regex).

 

The regular expression has to test true for the entire TextField content for the characters to be accepted.

 

Given how the docs read, I don't think a full regular expression string would be usable here, though I didn't Google for whether there's an escape hatch to allow it somehow.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Contributor
Posts: 8
Registered: ‎12-14-2010
My Device: Not Specified

Re: textField.restrict - how to just allow one decimal

Thank you DarkIn!!! 

Problem solved.

 

I did the following after trying out your code, so I could keep the white input boxes.

 

var TitleTB:TextInput;

TitleTB = new TextInput();
TitleTB.width = 160;
TitleTB.x = 10;
TitleTB.y = 10;
TitleTB.height = 25;
TitleTB.textField.restrict = ".0-9";
TitleTB.addEventListener(TextEvent.TEXT_INPUT, restrictOneDecimal);
addChild(TitleTB);

Developer
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900
My Carrier: Rogers

Re: textField.restrict - how to just allow one decimal

[ Edited ]

I have another solution if anybody want's to implement it.

I have a custom number pad on my screen and also wanted to restrict the input to 1 decimal as well.

 

So with my input text field titled "textInput"...I did the following in the event listener.

 

 

private function numPadDecimalClicked(e:MouseEvent):void {
			
			if(textInput.textField.text.indexOf(".") != -1) {
				e.preventDefault();
			} else {
				textInput.textField.appendText(".");
			}			
		}

 

Hope that helps someone else.

 

G