Validator (2) custom

Source: Internet
Author: User

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 "*"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.