The following example shows how you can validate strings using the static StringValidator.validateString() method.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/13/validating-strings-using-the-stringvalidator-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;
            import mx.controls.Alert;

            private function doValidate(value:String):void {
                var arr:Array = StringValidator.validateString(stringValidator, value);
                textArea.text = ObjectUtil.toString(arr);
                dataGrid.dataProvider = arr;
                if (arr.length == 0) {
                    Alert.show("String is valid.");
                }
            }
        ]]>
    </mx:Script>

    <mx:StringValidator id="stringValidator"
            minLength="3"
            maxLength="6" />

    <mx:ApplicationControlBar dock="true">
        <mx:TextInput id="textInput" />
        <mx:Button label="validate"
                click="doValidate(textInput.text);" />
    </mx:ApplicationControlBar>

    <mx:TabNavigator id="tabNavigator"
            creationPolicy="all"
            width="100%"
            height="100%">
        <mx:VBox label="Grid"
                width="100%"
                height="100%">
            <mx:DataGrid id="dataGrid"
                    variableRowHeight="true"
                    width="100%"
                    height="100%">
                <mx:columns>
                    <mx:DataGridColumn dataField="errorCode" />
                    <mx:DataGridColumn dataField="errorMessage"
                            wordWrap="true" />
                    <mx:DataGridColumn dataField="isError" />
                    <mx:DataGridColumn dataField="subField" />
                </mx:columns>
            </mx:DataGrid>
        </mx:VBox>
        <mx:VBox label="Text"
                width="100%"
                height="100%">
            <mx:TextArea id="textArea"
                    editable="false"
                    width="100%"
                    height="100%" />
        </mx:VBox>
    </mx:TabNavigator>

</mx:Application>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree