Custom flex data verification component!

Source: Internet
Author: User

Package mycompenent

{

Import MX. validators. validator; // reference the validator class.

Import MX. validators. validationresult; // reference the validationresult class.

Public class myvalidators extends validator

{

Public Function myvalidators () // Constructor

{

Super ();

}

Private var results: array; // defines an array to store errors.

// Rewrite the verification function

Override protected function dovalidation (value: Object): Array

{

VaR S: String = value as string;

Results = []; // clear the Array

Results = super. dovalidation (value); // verify it by using the dovalida tion Method in the inheritance class.

If (results. length> 0) // if an error occurs during verification, the error message is returned.

Return results;

If (S. length> 6) // custom verification. The character length cannot exceed 6

{

// Record error information

Results. Push (New validationresult (true, "text", "stringtoolong", "the character length exceeds 6 "));

}

Return results;

}

}

}

(5) The extends keyword indicates inheritance.

(6) The Super keyword indicates that the method in the subclass is inherited. Therefore, the "super. dovalidation (value)" statement indicates that the dovalidator method in the subclass is inherited.

(7) The override keyword indicates the rewrite function.

(8) The validationresult class is the verification result class, which is included in "MX. validators. The constructor of the validationresult class has four parameters. The first parameter indicates whether the error type is used, the second parameter indicates a specific attribute, the third parameter indicates the error type, and the fourth parameter indicates the error prompt.

(9) The "stringtoolong" error type is customized in this program, and the message "the character length exceeds 6" is displayed ". This error occurs when the data component length exceeds 6 characters.

(4) Call the custom verification class.

You must specify the namespace when calling a custom component. The components provided by Flex 3.0 are all under the MX namespace, so the xmlns: MX = "http://www.adobe.com/2006/mxml" statement is automatically generated after the mxml is created.

The syntax for referencing a custom component is as follows.

<Outermost component... Xmlns: space name = "package to which the class belongs">

...

<Space name: Custom component/>

The space name can be any name. The package keyword of the class must be the same as that of the class definition.

The following code references the custom component myvalidators.

<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml"

Xmlns: mycomp = "mycompenent. *">

...

<Mycomp: myvalidator... />

The following code calls the myvalidator component to verify the data in the input box.

<? XML version = "1.0"?>

<Mx: applicationxmlns: MX = "http://www.adobe.com/2006/mxml" xmlns: mycomp = "mycompenent. *" fontsize = "13">

<! -- Custom component myvalidators, used to verify string data -->

<Mycomp: myvalidators id = "myval"

Source = "{txtstring}" property = "text"

/>

<Mx: paneltitle = "Custom verification component" width = "300" Height = "200" horizontalalign = "center" verticalalign = "Middle">

<Mx: textinput id = "txtstring"/> <! -- Input box component -->

<Mx: Label text = "{txtstring. errorstring}"/> <! -- Label component, used to display verification results -->

<Mx: button label = "verify" Click = "myval. Validate ();"/> <! -- Button component to trigger verification -->

</MX: Panel>

</MX: Application>

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.