现在的位置: 首页 > 综合 > 正文

flex写的验证码

2013年08月18日 ⁄ 综合 ⁄ 共 1740字 ⁄ 字号 评论关闭

//======================单独写一个as类获取验证码=============

package file
{
 public class FlexCode
 {
  public function FlexCode()
  {
  }
  /**
   * 根据给定的位数获得验证码
   * @param count 验证码数量
   * @return 验证码字符串
   */
  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)
    {
     //数字从48开始(48的keycode是0)
     code=code+String.fromCharCode(48+(num%10));
    }
    else
    { //65大写字母,96小写字母
     code=code+String.fromCharCode(65+(num%26));
    }
   }
   trace("生成-->"+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;
    //在label上显示验证码
    var code:String="";
    public function initCode():void{
     code=FlexCode.getCode(4);
     this.lblcode.text=code;

    //给label画背景色
     lblcode.graphics.beginFill(0xff0000);
     lblcode.graphics.drawRect(0,0,lblcode.width,lblcode.height);
     lblcode.graphics.endFill();
    }
    //看不清事件,重新获得验证码
    private function refech():void{
     this.initCode();
    }
    //验证用户输入是否是相同
    private function validate():void{
     if(txtCode.text.toUpperCase()==code){
      Alert.show("验证通过!");
     }else{
      Alert.show("验证失败");
     }
    }
   ]]>
  </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="看不清楚?" 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>

 

抱歉!评论已关闭.