Solved

regular Expression validator in actionscript

Posted on 2010-11-23
5
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
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…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

696 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