Solved

regular Expression validator in actionscript

Posted on 2010-11-23
5
615 Views
Last Modified: 2013-11-11
Hello,

i have an application, containing a single item into which a certain regular expression can be entered..

<mx:Form>
                  <mx:FormItem label="validate:">
                        <s:TextInput id="dateBT" width="200" focusOut="dateTI_focusOutHandler(event)"/>
                  </mx:FormItem>
</mx:Form>

that is my view. the User Interface..

here is my declaration blck :

<fx:Declarations>
            <mx:RegExpValidator id="regVal" property="text" expression="{regExpr}"
                         flags="g"  source="{dateBT}" triggerEvent="focusOut"/>
      </fx:Declarations>

and i have var regExpr:String = "^\d{4}-(\d|0\d|1[012])-(\d|[012]\d|3[01]|today)$";

now, here is the test format:

aaaa22efe// not allowed
20100103 //YYYYMMDD not allowed
2010-01-03 //YYYY-MM-DD

so, only dates with the format YYYY-MM-DD should be validated ..as it is now, my code invalidates all three formats..i don't know what the problem could be..anybody with a clue please let me know.

thanks
0
Comment
Question by:admindesk
  • 2
5 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 total points
ID: 34200428
If the value "today" should be allowed by itself, then you want this pattern:

^\d{4}-(\d|0\d|1[012])-(\d|[012]\d|3[01])|today$

But that's not going to fix your problem - 2010-01-03 is valid with the pattern you provided, so there must be a problem somewhere else in the code. I'm not familiar with actionscript, so I won't be able to help any further probably.
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 250 total points
ID: 34212808
This is a total cop-out, but maybe you haven't done this yet. I'm going to paste in a working example that does exactly what you're taking about.

Do note that in this example, we use a couple more properties on the RegExpValidator declaration: valid, invalid and trigger. "trigger" is used to specify the component that originates the event that triggers the validator (in this case, dateBT).
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the RegExpValidator. -->
<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">

    <fx: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 = "";
                }
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:RegExpValidator id="regExpV" 
                source="{regex_text}" property="text" 
                flags="g" expression="{regex.text}" 
                valid="handleResult(event)"
                invalid="handleResult(event)"
                trigger="{myButton}"
                triggerEvent="click"/>
    </fx:Declarations>

    <s:Panel title="RegExpValidator Example"
            width="75%" height="75%"
            horizontalCenter="0" verticalCenter="0">
        <s:VGroup left="10" right="10" top="10" bottom="10">
            <s:Label width="100%" text="Instructions:"/>
            <s:Label width="100%" text="1. Enter text to search. By default, enter  a string containing the letters ABC in sequence followed by any digit."/>
            <s:Label width="100%" text="2. Enter the regular expression. By default, enter ABC\d."/>
            <s:Label width="100%" text="3. Click the Button control to trigger the validation."/>
            <s:Label 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:">
                    <s:TextInput id="regex_text" text="xxxxABC4xxx" width="100%"/>
                </mx:FormItem>
                <mx:FormItem label="Enter regular expression:">
                    <s:TextInput id="regex" text="ABC\d" width="100%"/>
                </mx:FormItem>
                <mx:FormItem label="Results:">
                    <s:TextInput id="reResults" width="100%"/>
                </mx:FormItem>
                <mx:FormItem >
                    <s:Button id="myButton" label="Validate"/>
                </mx:FormItem>
            </mx:Form>
        </s:VGroup>
    </s:Panel>

</s:Application>

Open in new window



0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 34212823
Other than the canned code, it's hard to pinpoint where the error is because we're not seeing all your code. For example what's happening inside your dateTI_focusOutHandler function? And how do you know your validation is not working? You RegExpValidator is not triggering any events...
0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 34424439
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Some Flash in IE doesn't display properly, but does in Chrome 4 636
AWAY 3d Using BitMapCubeTexture 1 423
Software Restriction Policy 8 881
Play RTMP without Flash 3 745
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question