| 包 | mx.validators |
| 类 | public class RegExpValidator |
| 继承 | RegExpValidator Validator EventDispatcher Object |
expression 属性将正则表达式传递给验证程序,然后使用 flags 属性传递用于控制正则表达式模式匹配情况的其它标志。
如果验证程序可以在要验证的字段中找到正则表达式的匹配项,则验证成功。如果验证程序找不到匹配项,则会出现验证错误。
RegExpValidator 类调度 valid 和 invalid 事件。对于 invalid 事件,事件对象是 ValidationResultEvent 类的实例,并且它包含 ValidationResult 对象的数组。
但是,对于 valid 事件,ValidationResultEvent 对象包含 RegExpValidationResult 对象的数组。RegExpValidationResult 类是 ValidationResult 类的子类,并且包含与正则表达式一起使用的其它属性,其中包括:
matchedIndex 包含匹配项输入字符串中的起始索引的整数。
matchedString 包含与正则表达式匹配的输入字符串的子字符串的字符串。
matchedSubStrings 包含用括弧括住的子字符串匹配项(如果有)的字符串数组。如果未找到子字符串匹配项,则此数组的长度为 0。使用 matchedSubStrings[0] 访问第一个子字符串匹配项。
隐藏 MXML 语法The <mx:RegExpValidator> tag
inherits all of the tag attributes of its superclass,
and adds the following tag attributes:
<mx:RegExpValidator
expression="No default"
flags="No default"
noExpressionError="The expression is missing."
noMatchError="The field is invalid."
/>
另请参见
| 属性 | 定义方 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
![]() | enabled : Boolean 将此值设置为 false 将停止验证程序执行验证操作。 | Validator | |
| expression : String 要用于验证的正则表达式。 | RegExpValidator | ||
| flags : String 匹配时使用的正则表达式标志。 | RegExpValidator | ||
![]() | listener : Object 指定验证侦听器。 | Validator | |
| noExpressionError : String 未指定任何正则表达式时的错误消息。 | RegExpValidator | ||
| noMatchError : String 没有正则表达式的匹配项时的错误消息。 | RegExpValidator | ||
![]() | property : String 一个字符串,用于指定包含要验证的值的 source 对象的属性名称。 | Validator | |
![]() | prototype : Object [静态]
对类或函数对象的原型对象的引用。 | Object | |
![]() | required : Boolean = true 如果为 true,则指定缺少值或空值将导致验证错误。 | Validator | |
![]() | requiredFieldError : String 缺少值和 required 属性为 true 时的错误消息。 | Validator | |
![]() | source : Object 指定包含要验证的属性的对象。 | Validator | |
![]() | trigger : IEventDispatcher 指定生成触发验证程序的组件。 | Validator | |
![]() | triggerEvent : String 指定触发验证的事件。 | Validator | |
| 方法 | 定义方 | ||
|---|---|---|---|
构造函数 | RegExpValidator | ||
![]() | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void 使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知。 | EventDispatcher | |
![]() | 将事件调度到事件流中。 | EventDispatcher | |
![]() | 检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。 | EventDispatcher | |
![]() |
指示对象是否已经定义了指定的属性。 | Object | |
![]() | 如果 Validator 是使用 MXML 标签创建的,则由 MXML 编译器自动调用。 | Validator | |
![]() |
指示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
指示指定的属性是否存在、是否可枚举。 | Object | |
![]() | 从 EventDispatcher 对象中删除侦听器。 | EventDispatcher | |
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() | 执行验证并(可选)将结果通知给侦听器。 | Validator | |
![]() | [静态] 调用验证程序数组中的所有验证程序。 | Validator | |
![]() |
返回指定对象的原始值。 | Object | |
![]() | 检查是否用此 EventDispatcher 对象或其任何始祖为指定事件类型注册了事件侦听器。 | EventDispatcher | |
| 方法 | 定义方 | ||
|---|---|---|---|
![]() | 为从验证程序调度的有效和无效事件设置所有侦听器。 | Validator | |
覆盖基类 doValidation() 方法以验证正则表达式。 | RegExpValidator | ||
![]() | 返回要验证的对象。 | Validator | |
![]() | 从错误结果数组返回 ValidationResultEvent。 | Validator | |
![]() | 如果值不为 null,则返回 true。 | Validator | |
![]() | 断开从验证程序调度的有效和无效事件的所有侦听器的连接。 | Validator | |
![]() | 构造 Validator 后,每当 ResourceManager 又调度一个“change”事件以指示已通过某种方式更改了本地化资源时,将会调用此方法。 | Validator | |
| expression | 属性 |
expression:String [读写] 要用于验证的正则表达式。
public function get expression():String public function set expression(value:String):void| flags | 属性 |
flags:String [读写] 匹配时使用的正则表达式标志。
public function get flags():String public function set flags(value:String):void| noExpressionError | 属性 |
noExpressionError:String [读写] 未指定任何正则表达式时的错误消息。默认值为“The expression is missing”。
public function get noExpressionError():String public function set noExpressionError(value:String):void| noMatchError | 属性 |
noMatchError:String [读写] 没有正则表达式的匹配项时的错误消息。默认值为“The field is invalid”。
public function get noMatchError():String public function set noMatchError(value:String):void| RegExpValidator | () | 构造函数 |
public function RegExpValidator()构造函数
| doValidation | () | 方法 |
override protected function doValidation(value:Object):Array
覆盖基类 doValidation() 方法以验证正则表达式。
不要直接调用此方法;Flex 会在执行验证的过程中调用它。如果创建自定义 Validator 类,则必须实现此方法。
参数
value:Object — 要验证的对象。 |
Array — 对于无效结果,为 ValidationResult 对象的数组,其中包含验证程序所检查的每个字段的一个 ValidationResult 对象。 |
<?xml version="1.0"?>
<!-- Simple example to demonstrate the RegExpValidator. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
import mx.validators.*;
// Write the results to the
private function handleResult(eventObj:ValidationResultEvent):void {
if (eventObj.type == ValidationResultEvent.VALID)
{
// For valid events, the results Array contains
// RegExpValidationResult objects.
var xResult:RegExpValidationResult;
reResults.text="";
for (var i:uint = 0; i < eventObj.results.length; i++)
{
xResult = eventObj.results[i];
reResults.text=reResults.text + xResult.matchedIndex + " " +
xResult.matchedString + "\n";
}
}
else
{
reResults.text="";
}
}
]]>
</mx:Script>
<mx:RegExpValidator id="regExpV"
source="{regex_text}" property="text"
flags="g" expression="{regex.text}"
valid="handleResult(event)" invalid="handleResult(event)"
trigger="{myButton}" triggerEvent="click"/>
<mx:Panel title="RegExpValidator Example" width="95%" height="95%"
paddingTop="5" paddingLeft="5" paddingRight="5" paddingBottom="5">
<mx:Text width="100%" text="Instructions:"/>
<mx:Text width="100%" text="1. Enter text to search. By default, enter a string containing the letters ABC in sequence followed by any digit."/>
<mx:Text width="100%" text="2. Enter the regular expression. By default, enter ABC\d."/>
<mx:Text width="100%" text="3. Click the Button control to trigger the validation."/>
<mx:Text width="100%" text="4. The results show the index in the text where the matching pattern begins, and the matching pattern. "/>
<mx:Form>
<mx:FormItem label="Enter text: ">
<mx:TextInput id="regex_text" text="xxxxABC4xxx" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Enter regular expression: ">
<mx:TextInput id="regex" text="ABC\d" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Results: ">
<mx:TextInput id="reResults" width="100%"/>
</mx:FormItem>
<mx:FormItem >
<mx:Button id="myButton" label="Validate"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>