RegExpValidator in Flex4


 

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 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>

0

<mx:RegExpValidator id="regExpValidator"

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

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

1

invalid="Result(event)"

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

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

2

</fx:Declarations>

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

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

3

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

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

</mx:FormItem>

4

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

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

</mx:FormItem>

5

<mx:FormItem label="Result: ">

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

</mx:FormItem>

6

<mx:FormItem >

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

</mx:FormItem>

7

</mx:Form>

</s:Panel>

</s:Application>

8

Output:

Running Application:

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

Download this code

Ads