Dataverify. mxml
<? XML version = "1.0"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns: myv = "myvalidators. * "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, namev];
}
// 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"/>
<Myv: namevalidator id = "namev" Source = "{txtname}" property = "text" requiredfielderror = "cannot be blank"/>
<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: formitem label = "name:" required = "true">
<Mx: textinput id = "txtname"/>
</MX: formitem>
</MX: Form>
<Mx: button label = "Validate" Click = "btnvalidate_click (event)"/>
</MX: Application>
Namevalidator.
Package myvalidators
{
Import MX. Controls. text;
Import MX. validators. validator;
Import MX. validators. validationresult;
Public class namevalidator extends validator
{
// Define array for the return value of dovalidation ().
Private var results: array;
Public Function namevalidator ()
{
Super ();
}
Override protected function dovalidation (value: Object): Array
{
VaR name: String = value as string;
// Clear results array.
Results = [];
// Call base class dovalidation ().
Results = super. dovalidation (value );
// Return if there are errors.
If (results. length> 0)
Return results;
// Check Name field.
If (name! = "Huangxi ")
{
Results. Push (New validationresult (true,
"Name", "wrongname", "Please enter the name 'huangsi '."));
Return results;
}
Return results;
}
}
}
//////////////////////////////////
Summary
1. Create a custom namevalidator to inherit validator, override dovalidation (), and add the custom method to this function.
2. Empty error messages are not allowed. The requiredfielderror attribute is available.
3. <mx: formitem> has the required attribute. If it is set to true, "*" is displayed "*"