Home Tutorial Flex Flex4 Components RegExpValidator in Flex4

 
 

RegExpValidator in Flex4
Posted on: May 28, 2010 at 12:00 AM
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

Related Tags for RegExpValidator in Flex4:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.