Flex2 development projects often need to verify user input. flex2 itself provides us with rich components.
The following example provides the following features:
1) centralized data verification
2) locate the cursor to the first verification failure location
3) CSS displayed in Chinese in the error message
<? 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>
Example: