PDA

View Full Version : [AS3] how can i add custom validation on TextInput?


genxsol
06-23-2009, 03:54 PM
Hi Dear,

i am trying to figure out since morning how can i add a validation function on a TextInput in as3.
i make some TextInput dynamically in as3 and want to add a validation on focus change.

can anybody help me ?

MikeHovey
06-23-2009, 04:09 PM
What kind of validation are you looking for? And are you using Flash or Flex?

genxsol
06-24-2009, 09:44 AM
i am interested in empty field and email validation at the moment in flex.

i generate dynamic textInput in action script like this

public function generateUIFromFields( fields : Array ) : void {

for ( var i = 0; i < fields.length; i++ ) {
var field : Field = fields[ i ];
trace( "type: " + field.type )
var uiComponent : UIComponent = createUIComponent( field );

view.addChild( uiComponent );
}

}

public function createUIComponent( field : Field ) : FormItem {

var control : FormItem = new FormItem();
control.percentHeight = 100;
control.percentWidth = 100;
control.name = field.id;
control.required = true;

switch ( field.type ) {
case Field.TYPE_TEXT:

var textField : TextInput = new TextInput();


textField.percentWidth = 50;
textField.name = field.id;

if ( field.name != null ) {
control.label = field.name;
}

control.addChild( textField );
break;

case Field.TYPE_PASSWORD:

var passwordField : TextInput = new TextInput();
passwordField.percentWidth = 50;
passwordField.name = field.id;

if ( field.name != null ) {
control.label = field.name;
}

passwordField.displayAsPassword = true;

control.addChild( passwordField );
break;
}
return control;
}

it generates number of TextInput filed we required and then the next function capture input value from them.

i create a loop on all the fields and check if its empty.

var formField : DisplayObjectContainer = view.getChildByName( field.id ) as DisplayObjectContainer;
var viewField : DisplayObject = formField.getChildByName( field.id );

//////////////////////////////////////

this checks all the fileds when i click on validate button but i want to trigger a validate function run time when user go to next field.

MikeHovey
06-24-2009, 06:09 PM
Flex has great validators that you should take a look at. Using those, there is no need to use FOCUS_IN and FOCUS_OUT event handlers... Flex will do it on its own.

http://livedocs.adobe.com/flex/2/langref/mx/validators/Validator.html
http://livedocs.adobe.com/flex/2/langref/mx/validators/EmailValidator.html
http://livedocs.adobe.com/flex/2/langref/mx/validators/StringValidator.html

Here's an example of how to use them:

<mx:EmailValidator
id="emailValidator"
requiredFieldError="Your email address is required."
invalidCharError="You have entered an invalid email address."
invalidDomainError="You have entered an invalid email address."
invalidIPDomainError="You have entered an invalid email address."
invalidPeriodsInDomainError="You have entered an invalid email address."
missingAtSignError="You have entered an invalid email address."
missingPeriodInDomainError="You have entered an invalid email address."
missingUsernameError="You have entered an invalid email address."
tooManyAtSignsError="You have entered an invalid email address."
source="{emailInput}"
property="text" />

<mx:StringValidator
id="nameValidator"
requiredFieldError="Your name is required."
source="{this.nameInput}"
property="text" />

You can adjust any of the error messages to say whatever you want. The beauty of these validators is as a user tabs out of the input field, if the validation requirements are not met, the field will highlight and a ErrorTip, similar to a ToolTip, will be displayed to the right of the field with the message you provided.

There are also a bunch of other validators such as phone number, credit card, zip code, etc.

SevenDeluxe
07-07-2009, 05:30 PM
Are those only in Flex? Is there anything similar in Flash?