Solved

regular Expression validator in actionscript

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
3D rotation for a product 3 339
Youtube video error 30 772
blocked plugin flash 5 117
Shockwave Flash Object within PowerPoint 2013 2 129
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…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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 set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

770 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