An example of using validator
/// // <? XML version = "1.0"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns: Local = "*" creationcomplete = "initvalidatorarray ();" width = "402" Height = "152">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Events. validationresultevent;
Import MX. Core. uicomponent;
Import MX. Controls. Alert;
Import MX. validators .*;
Private var myvalidators: array;
Private function initvalidatorarray (): void {
Myvalidators = [zipv, PNV];
}
// Button event, detected by performing the operation
Private function btnvalidate_click (Event: Event): void {
VaR validatorresults: array = mx. validators. validator. validateall (myvalidators );
// If the length is 0, all verification passes
If (validatorresults. Length = 0 ){
MX. Controls. Alert. Show ("Validate OK! ";
} Else {
// Locate the cursor to the first error
VaR V: validationresultevent = validatorresults [0] As validationresultevent;
(V.tar get. source as uicomponent). setfocus ();
}
}
]>
</MX: SCRIPT>
<! -- If you want to display Chinese characters in the verification error prompt, use this to change the font size! -->
<Mx: style>
. Errortip {fontfamily: "simsun"; fontsize: "12 "}
</MX: style>
<Mx: zipcodevalidator id = "zipv" Source = "{zipcodeinput}" property = "text" requiredfielderror = "zip code required"/>
<Mx: phonenumbervalidator id = "PNV" Source = "{phonenumberinput}" property = "text"/>
<Mx: form x = "24" Y = "24">
<Mx: formitem label = "zip code:" required = "true">
<Mx: textinput id = "zipcodeinput"/>
</MX: formitem>
<Mx: formitem label = "phone number:" required = "true">
<Mx: textinput id = "phonenumberinput"/>
</MX: formitem>
</MX: Form>
<Mx: button label = "Validate" Click = "btnvalidate_click (event)"/>
</MX: Application>
/////////////////
Summary
1. The advantage of validator is that you can bind the validation of input to the control to avoid confusion in the program logic.
2. Multiple validators can be processed in a centralized manner. myvalidators = [zipv, PNV];
// Put the validation error result in array
VaR validatorresults: array = mx. validators. validator. validateall (myvalidators );
3. Modify the errortip Style