Validator (1)

Source: Internet
Author: User
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

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.