Solved

regular Expression validator in actionscript

Posted on 2010-11-23
5
607 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Embedding Flash App/Movie on Website 3 484
Stock Videos 17 319
Begin mp4 video on first frame 6 163
EMET 5.2 / Adobe Flash 2 219
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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): …
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now