PDA

View Full Version : TextInput control


JoeDeveloper
11-01-2007, 02:54 PM
I have a TextInput box for a user to enter a number of the format XXXX.XXX

The user enters both the decimal point and the numbers (0-9). The largest the number can get is 9999.999. Is there any way in which I can put a restriction on this TextInput box for the parameters I listed above (users can't enter more than four numbers before the decimal point and no more than three numbers after inputting a decimal point)?:confused:

I've tried manipulating a few things (ie, restrict, maxChars, etc) but I can't find anything that works. I am stuck between a rock and a hard place.

Other ideas are welcomed as long as I get the format above!

Thanks

Can I
11-01-2007, 04:23 PM
the solution would be to create your own Validator and use it with TextInput control

// actually there already is a RegExpValidator (http://livedocs.adobe.com/flex/201/langref/mx/validators/RegExpValidator.html) you can use

JoeDeveloper
11-01-2007, 06:04 PM
RegExpValidator only validates if you give it an expression to compare against. I don't know what a user will put it because the user can input anywhere from 0-9999.999. They don't even have to put in a decimal point in if there is none, so a user can also input something like 3454. The main thing is that I contain the user to XXXX.XXX format.

Or am I understanding RegExpValidator completely wrong?

Thanks for the above help though.

krullwarking
11-01-2007, 07:19 PM
RegExpValidator only validates if you give it an expression to compare against. I don't know what a user will put it because the user can input anywhere from 0-9999.999. They don't even have to put in a decimal point in if there is none, so a user can also input something like 3454. The main thing is that I contain the user to XXXX.XXX format.

Or am I understanding RegExpValidator completely wrong?

your validator only needs to be sure that the value is between 0 and 9999.999. Then you need a formatter that will do any formatting you want link pad with leading zeros and or add the decimal plus the trailing zero. the logic flows like this:

is the number between 0 and 9999.999
Yes:format the number nicely and return it. :::: see mx.formatters
No:Return an error

Can I
11-02-2007, 09:20 AM
@JoeDeveloper you've got it right, nonetheless you can write a regex that will do the job

krullwarking's method might be easier though

JoeDeveloper
11-02-2007, 12:34 PM
Can I use RegExpValidator to check if the values I'm getting are between 0 and 9999.999 or would I have to use RegEx?

How would I set up the expression (since the input can be within a range, I can not set an expression to an exact string)? Sample coding would help!

I'm a little new to FLEX so please bare with me.

Thanks for all the help so far everyone!

Can I
11-02-2007, 02:02 PM
Im not very good at regex myself, but use this to play with it: http://www.ultrapico.com/Expresso.htm

kreddy79
04-22-2010, 11:05 AM
private function validateDecimal(evt:Event,maxbefore:int,maxafter:i nt):void {
//RegExp for both Negative and Non Negative /^[+-]?\d{0,3}\.?\d{1,2}$/i
var expString:String = "^\\d{0,"+maxbefore+"}\\.?\\d{1,"+maxafter+"}$";
var decimalRegex:RegExp = new RegExp(expString,"i");
var inputText:String = TextInput(evt.currentTarget).text;
if(inputText && inputText.indexOf(",")>= 0 ){
var decimalSep:RegExp = /,/g;
inputText = inputText.replace(decimalSep,"");
}
if (decimalRegex.test(inputText)) {
TextInput(evt.currentTarget).errorString = null;
var myFormatter:NumberFormatter=new NumberFormatter();
myFormatter.precision=2;
myFormatter.useThousandsSeparator = true;
myFormatter.useNegativeSign = false;
TextInput(evt.currentTarget).text = myFormatter.format(inputText);
} else {
TextInput(evt.currentTarget).errorString = "Please enter valid number,should be between 0-99,999,999,999.99";
}
}
private function replaceComma(evt:Event):void{
var inputText:String = TextInput(evt.currentTarget).text;
if(inputText && inputText.indexOf(",")>= 0 ){
var decimalSep:RegExp = /,/g;
inputText = inputText.replace(decimalSep,"");
TextInput(evt.currentTarget).text = inputText;
}
}
<mx:TextInput maxChars="14" focusIn="replaceComma(event)" focusOut="validateDecimal(event,11,2)" restrict="0-9." />