download RegExpValidator.as
Language: ActionScript
Copyright: (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
LOC: 132
Project Info
Flex SDK(flex_sdk_2.zip)
Server: Adobe FLEX
Type: zip
...works\source\mx\validators\
   CreditCardValidator.as
   ...ardValidatorCardType.as
   CurrencyValidator.as
   ...ValidatorAlignSymbol.as
   DateValidator.as
   EmailValidator.as
   IValidatorListener.as
   NumberValidator.as
   PhoneNumberValidator.as
   RegExpValidationResult.as
   RegExpValidator.as
   SocialSecurityValidator.as
   StringValidator.as
   ValidationResult.as
   Validator.as
   ZipCodeValidator.as
   ...eValidatorDomainType.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
//  All Rights Reserved. The following is Source Code and is subject to all
//  restrictions on such code as contained in the End User License Agreement
//  accompanying this product.
//
////////////////////////////////////////////////////////////////////////////////

package mx.validators
{

import mx.events.ValidationResultEvent;
import mx.managers.ISystemManager;
import mx.managers.SystemManager;
import mx.resources.ResourceBundle;

/** 
 *  The RegExpValidator class lets you use a regular expression
 *  to validate a field. 
 *  You pass a regular expression to the validator using the
 *  <code>expression</code> property, and additional flags
 *  to control the regular expression pattern matching 
 *  using the <code>flags</code> property. 
 *
 *  <p>The validation is successful if the validator can find a match
 *  of the regular expression in the field to validate.
 *  A validation error occurs when the validator finds no match.</p>
 *
 *  <p>The RegExpValidator class dispatches the <code>valid</code>
 *  and <code>invalid</code> events.
 *  For an <code>invalid</code> event, the event object is an instance
 *  of the ValidationResultEvent class, and it contains an Array
 *  of ValidationResult objects.</p>
 *
 *  <p>However, for a <code>valid</code> event, the ValidationResultEvent
 *  object contains an Array of RegExpValidationResult objects.
 *  The RegExpValidationResult class is a child class of the
 *  ValidationResult class, and contains additional properties 
 *  used with regular expressions, including the following:</p>
 *  <ul>
 *    <li><code>matchedIndex</code> An integer that contains the starting 
 *      index in the input String of the match.</li>
 *    <li><code>matchedString</code> A String that contains the substring 
 *      of the input String that matches the regular expression.</li>
 *    <li><code>matchedSubStrings</code> An Array of Strings that contains 
 *      parenthesized substring matches, if any. If no substring matches are found, 
 *      this Array is of length 0.  Use matchedSubStrings[0] to access the 
 *      first substring match.</li>
 *  </ul>
 *  
 *  @mxml
 *
 *  <p>The <code>&lt;mx:RegExpValidator&gt;</code> tag 
 *  inherits all of the tag attributes of its superclass,
 *  and adds the following tag attributes:</p>
 *  
 *  <pre>
 *  &lt;mx:RegExpValidator
 *    expression="<i>No default</i>" 
 *    flags="<i>No default</i>" 
 *    noExpressionError="The expression is missing." 
 *    noMatchError="The field is invalid." 
 *  /&gt;
 *  </pre>
 *
 *  @includeExample examples/RegExValidatorExample.mxml
 *  
 *  @see mx.validators.RegExpValidationResult
 *  @see mx.validators.ValidationResult
 *  @see RegExp
 */
public class RegExpValidator extends Validator
{
    include "../core/Version.as";

    //--------------------------------------------------------------------------
    //
    //  Class initialization
    //
    //--------------------------------------------------------------------------

    loadResources();
    
    //--------------------------------------------------------------------------
    //
    //  Class resources
    //
    //--------------------------------------------------------------------------

    [ResourceBundle("validators")]
    
    /**
     *  @private    
     */
    private static var packageResources:ResourceBundle;
    
    /**
     *  @private    
     */
    private static var resourceNoExpressionError:String;

    /**
     *  @private    
     */
    private static var resourceNoMatchError:String;

    //--------------------------------------------------------------------------
    //
    //  Class methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @private    
     *  Loads resources for this class.
     */
    private static function loadResources():void
    {
        resourceNoExpressionError =
            packageResources.getString("noExpressionError");
                    
        resourceNoMatchError = packageResources.getString("noMatchError");
    }

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /** 
     *  Constructor
     */     
    public function RegExpValidator()
    {
        super();

        bundleChanged();
    }
    
    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /** 
     *  @private
     */     
    private var _regExp:RegExp;
    
    /** 
     *  @private
     */     
    private var _foundMatch:Boolean = false;
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  expression
    //----------------------------------

    /** 
     *  @private
     *  Storage for the expression property.
     */     
    private var _expression:String;
    
    [Inspectable(category="General")]

    /**
     *  The regular expression to use for validation. 
     */
    public function get expression():String
    {
        return _expression;
    }
        
    /** 
     *  @private
     */     
    public function set expression(value:String):void
    {
        if (_expression != value)
        {
            _expression = value;
            createRegExp();
        }
    }
    
    //----------------------------------
    //  flags
    //----------------------------------
    
    /** 
     *  @private
     *  Storage for the flags property.
     */     
    private var _flags:String;
    
    [Inspectable(category="General", defaultValue="null")]

    /**
     *  The regular expression flags to use when matching.
     */
    public function get flags():String
    {
        return _flags;
    }
    
    /** 
     *  @private
     */     
    public function set flags(value:String):void
    {
        if (_flags != value)
        {
            _flags = value;
            createRegExp();
        }
    }

    //--------------------------------------------------------------------------
    //
    //  Properties: Errors
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  noExpressionError
    //----------------------------------

    [Inspectable(category="Errors", defaultValue="The expression is missing.")]

    /** 
     *  Error message when there is no regular expression specifed. 
     *  The default value is "The expression is missing."
     */
    public var noExpressionError:String;

    //----------------------------------
    //  noMatchError
    //----------------------------------
        
    [Inspectable(category="Errors", defaultValue="The field is invalid.")]

    /** 
     *  Error message when there are no matches to the regular expression. 
     *  The default value is "The field is invalid."
     */
    public var noMatchError:String;
    
    //--------------------------------------------------------------------------
    //
    //  Overridden methods
    //
    //--------------------------------------------------------------------------

    /**
     *  Override of the base class <code>doValidation()</code> method
     *  to validate a regular expression.
     *
     *  <p>You do not call this method directly;
     *  Flex calls it as part of performing a validation.
     *  If you create a custom Validator class, you must implement this method. </p>
     *
     *  @param value Object to validate.
     *
     *  @return For an invalid result, an Array of ValidationResult objects,
     *  with one ValidationResult object for each field examined by the validator. 
     */
    override protected function doValidation(value:Object):Array
    {
        var results:Array = super.doValidation(value);
        
        // Return if there are errors
        // or if the required property is set to <code>false</code> and length is 0.
        var val:String = value ? String(value) : "";
        if (results.length > 0 || ((val.length == 0) && !required))
            return results;

        return validateRegExpression(value);
    }
    
    /**
     *  @private
     */
    override protected function handleResults(
                                    errorResults:Array):ValidationResultEvent
    {
        var result:ValidationResultEvent;
        
        if (_foundMatch)
        {
            result = new ValidationResultEvent(ValidationResultEvent.VALID);
            result.results = errorResults;
        }
        else
        {
            result = super.handleResults(errorResults);
        }
        
        _foundMatch = false;    
        
        return result;  
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @private    
     *  Populates localizable properties from the loaded bundle for this class.
     */
    private function bundleChanged():void
    {
        noExpressionError = resourceNoExpressionError;  
        noMatchError = resourceNoMatchError;
    }

    /**
     *  @private
     */
    private function createRegExp():void
    {
        _regExp = new RegExp(_expression,_flags);
    }
    
    /**
     *  @private 
     *  Performs validation on the validator
     */     
    private function validateRegExpression(value:Object):Array
    {
        var results:Array = [];
        _foundMatch = false;
    
        if (_regExp)
        {
            var result:Object = _regExp.exec(String(value));
            if (_regExp.global)
            {
                while (result != null) 
                {
                    results.push(new RegExpValidationResult(
                        false, null, "", "", result[0],
                        result.index, result.slice(1)));
                    result = _regExp.exec(String(value));
                    _foundMatch = true;
                }
            }
            else if (result != null)
            {
                results.push(new RegExpValidationResult(
                    false, null, "", "", result[0],
                    result.index, result.slice(1)));                
                _foundMatch = true;
            }   
            
            if (results.length == 0)
            {
                results.push(new ValidationResult(
                    true, null, "noMatch", noMatchError));
            }
        }
        else
        {
            results.push(new ValidationResult(
                true, null, "noExpression", noExpressionError));
        }
        
        return results;
    }
}

}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us