The following example shows you how you can validate email addresses in a Flex application using the EmailValidator class and the validate() and static EmailValidator.validateEmail() methods
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/29/validating-email-addresses-using-the-emailvalidator-class-in-flex/ -->
<mx:Application name="EmailValidator_validate_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
private function emailValidator_valid(evt:ValidationResultEvent):void {
textInput.errorString = "";
message.text = "";
Alert.show(textInput.text, "You entered a valid email address:");
}
private function emailValidator_invalid(evt:ValidationResultEvent):void {
textInput.errorString = evt.message;
message.text = evt.message;
}
private function btn_click(evt:MouseEvent):void {
emailValidator.validate(textInput.text);
}
]]>
</mx:Script>
<mx:EmailValidator id="emailValidator"
valid="emailValidator_valid(event);"
invalid="emailValidator_invalid(event);" />
<mx:Form defaultButton="{btn}">
<mx:FormItem label="email address:"
direction="horizontal">
<mx:TextInput id="textInput"
focusAlpha="0.5" />
<mx:Button id="btn"
label="validate"
click="btn_click(event);" />
</mx:FormItem>
</mx:Form>
<mx:Label id="message" />
</mx:Application>
No comments:
Post a Comment