// ========================================= Write a separate as class to obtain the verification code ================ ====
Package File
{
Public class flexcode
{
Public Function flexcode ()
{
}
/**
* Obtain the verification code based on the given number of digits.
* @ Param count number of verification Codes
* @ Return verification code string
*/
Public static function getcode (count: INT): String
{
VaR code: String = "";
VaR num: Number = 0;
For (var I: Int = 0; I <count; I ++)
{
Num = math. Floor (math. Random () * 10000 );
If (Num % 2 = 0)
{
// The number starts from 48 (the keycode of 48 is 0)
Code = code + String. fromcharcode (48 + (Num % 10 ));
}
Else
{// 65 uppercase letters, 96 lowercase letters
Code = code + String. fromcharcode (65 + (Num % 26 ));
}
}
Trace ("generate -->" + Code );
Return code;
}
}
}
// ====================== Mxml ==============================
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" width = "939" layout = "absolute" Height = "517">
<Mx: style>
. Linkbutton {text-Decoration: underline; font-style: italic ;}
</MX: style>
<Mx: SCRIPT>
<! [CDATA [
Import file. flexcode;
// Display the verification code on the label
VaR code: String = "";
Public Function initcode (): void {
Code = flexcode. getcode (4 );
This. lblcode. Text = code;
// Draws the background color for the label.
Lblcode. Graphics. beginfill (0xff0000 );
Lblcode. Graphics. drawrect (0, 0, lblcode. Width, lblcode. Height );
Lblcode. Graphics. endfill ();
}
// Check the event and obtain the verification code again.
Private function refech (): void {
This. initcode ();
}
// Verify that user input is the same
Private function validate (): void {
If (txtcode. Text. touppercase () = Code ){
Alert. Show ("verified! ");
} Else {
Alert. Show ("Verification Failed ");
}
}
]>
</MX: SCRIPT>
<Mx: label x = "227" Y = "440" text = "label" width = "77" fontsize = "18" id = "lblcode" fontstyle = "italic"/>
<Mx: linkbutton x = "312" Y = "438" label = "cannot see clearly? "Color =" #031af8 "fontsize =" 18 "stylename =" linkbutton "creationcomplete =" initcode () "Click =" refech () "/>
<Mx: textinput x = "46" Y = "446" id = "txtcode"/>
<Mx: button x = "60" Y = "485" label = "Validate" width = "102" Click = "Validate ()"/>
</MX: Application>