Share on Google+Share on Google+

RegExpValidator in Flex4

Advertisement
The RegExpValidator class uses a regular expression to validate a field. You can define a regular expression to the validator using expression property.

RegExpValidator in Flex4:

The RegExpValidator class uses a regular expression to validate a field. You can define a regular expression to the validator using expression property. The validation will be successful if the field value matches with the regular expression.
If the value does not match an error message occur.
The tag of RegExpValidator  is <mx:RegExpvalidator>.
In this example you can see how we can use RegExpValidator.

Example:

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"

minWidth="955" minHeight="600">

<fx:Script>

<![CDATA[

import mx.controls.Alert;

import mx.events.ValidationResultEvent;

import mx.validators.*;

private function Result(Obj:ValidationResultEvent):void {

if (Obj.type == ValidationResultEvent.VALID)

{

var result:RegExpValidationResult;

resulttxt.text="x";

for (var i:uint = 0; i < Obj.results.length; i++)

{

result = Obj.results[i];

resulttxt.text=resulttxt.text + result.matchedIndex + " " + result.matchedString;

}

}

else

{

resulttxt.text="";

}

}

]]>

</fx:Script>

<fx:Declarations>

<mx:RegExpValidator id="regExpValidator"

source="{regtxt}" property="text"

flags="g" expression="{regextxt.text}"

invalid="Result(event)"

trigger="{btn}" triggerEvent="click"

valid="Alert.show('Validation Successful'), Result(event);"/>

</fx:Declarations>

<s:Panel title="RegExpValidator Example" width="550">

<mx:Form width="548" backgroundColor="#101446" color="#FFFFFF">

<mx:FormItem label="Enter text: ">

<s:TextInput id="regtxt" text="xxxxABC4xxx" width="100%" color="#000000"/>

</mx:FormItem>

<mx:FormItem label="Enter regular expression: ">

<s:TextInput id="regextxt" text="ABC\d" width="100%" color="#000000"/>

</mx:FormItem>

<mx:FormItem label="Result: ">

<s:TextInput id="resulttxt" width="100%" color="#000000"/>

</mx:FormItem>

<mx:FormItem >

<s:Button id="btn" label="Check Validation" chromeColor="#5F1B0C"/>

</mx:FormItem>

</mx:Form>

</s:Panel>

</s:Application>

Output:

Running Application:

To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.

Download this code

Advertisements

Advertisement

Posted on: May 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.