[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

regular Expression validator in actionscript

Posted on 2010-11-23
5
Medium Priority
?
623 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
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…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

650 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