Flex custom validator

Source: Internet
Author: User

Reprinted from http://amberoracle.blog.163.com/blog/static/10598633820097262242474/

Step 1: Write the as class to inherit MX. validators. validator and import MX. validators. validationresult.
Value stores the content to be verified
The results are stored in the Results array. If verification is successful, the data is empty. If verification fails, the array contains the validationresult. object, which contains the failure information.

Package utils
{
Imp ort MX. validators. validator;// The validator class is for all flex VerificationProgram.

IMP Ort mx. validators. validationresult;

Public class addressvalidator extends validator
{
Private var results: array;
Public Function addressvalidator ()
{
Super ();
}
Protected override function dovalidation (value: Object): Array { // Protected method protect; dovalidation (value: Object): array; execute the verification logic of this verification program, this includes verifying whether missing values or null values cause verification errors defined by the value of the required attribute.
Results = [];
Results = super. dovalidation (value );
If (value! = NULL)
{
VaR pattern: Regexp = new Regexp ("\ D + \ x20 [A-Za-Z] + ");
If (value. Search (pattern) =-1 ){
Results. Push (New validationresult (true, null, "notaddress", "This is not a valid address "));
// Error Message
}
}
Return results; // If the returned string is null, it passes verification. If there is any content, it is the result of verification failure.
}
}
}

Step 2: import the flex Program
Xmlns: U = "utils .*"
Create validators
<U: addressvalidator id = "addressv" Source = "{billingaddress}" property = "text" required = "true"/>
Id validator name
Source verification Source
Property Verification source location property name
Required cannot be blank

Step 3: Import IMP in the flex Program Ort mx. Events. validationresultevent;
To determine the verification results.
Private var vaddressresult: validationresultevent = NULL;

Inspection:
Private function process (): void {
Vaddressresult = addressv. Validate (); // The validator performs the validate () method for verification. // Validate () method
// Public function validate (value: Object = NULL, suppressevents: Boolean = false): validationresultevent
// Perform the verification and (optional) notify the listener of the result.

If (vaddressresult. type = validationresultevent. valid) { // determine whether verification is successful
var E: objectdataevent = new objectdataevent ("billinginforeturn", checkoutinfo);
This. dispatchevent (E);
}< BR >}

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.