Validating strings using the StringValidator class

by Peter deHaan on March 13, 2008

in StringValidator, Validators

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>

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: