Validating strings using the StringValidator class

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>