代码改变世界

验 正

2010-03-25 20:33  宝宝合凤凰  阅读(250)  评论(0)    收藏  举报
哈哈,是不是觉得最后那个验证码很变态=。=

来看下Demo部分的代码:
01.import org.nwhy.utils.Captcha;
02.var cpTypes:Array=["number","character","math","bt"];
03.var aryCps:Array=[];
04.var cpNum:uint=4;
05.init();
06.function init() {
07.    for (var i:uint=0; i<cpnum ; i++) {
08.        var cp:Captcha=new Captcha(cpTypes[i]);
09.        cp.x=130;
10.        cp.y=40*i+15;
11.        cp.addEventListener("change",cpChange);
12.        addChild(cp);
13.        aryCps.push(cp);
14.        this["txt_ipt"+i].addEventListener("change",iptChange);
15.    }
16.}
17.function cpChange(_e:Event) {
18.    var obj:Captcha=_e.currentTarget as Captcha;
19.    trace(obj.captcha);
20.}
21.function iptChange(_e:Event) {
22.    var obj:TextField=_e.currentTarget as TextField;
23.    var num:uint=_e.currentTarget.name.substr(7);
24.    if (obj.text==aryCps[num].captcha) {
25.        this["txt_msg"+num].text="验证通过";
26.    } else {
27.        this["txt_msg"+num].text="验证失败";
28.    }
29.}

Captcha类代码如下:

001./*
002.VERSION: 1.0 DATE:2009/11/20
003.ACTIONSCRIPT VERSION: 3.0
004.AUTHOR: DFdou, k5love@foxmail.com
005.Copyright 2009, nwhy.org. All rights reserved.
006.  
007.DESCRIPTION:
008.    Captcha is a simple class to generate Caphtcha Sprite.
009.  
010.PARAMETERS:
011.    1) type:String="number": Captcha type you want to create, there are four types ["number","character","math","bt"];
012.    2) length:uint=5: Captcha length,work with type ["number","character"];
013.    3) noiseFlag:Boolean=true: if true Captcha will draw noise;
014.    4) size:uint=18: textfiled's fontsize;
015.  
016.NOTES:
017.    - when the Captcha was changed,will dispatch an event named "change",then you can addEventListener to capture it.
018.  
019.EXAMPLE:
020.    var cp:Captcha = new Captcha(); //default Captcha as number type width noise,length=5,size=18;
021.    var cp:Captcha = new Captcha("character", 4); //character type Captcha,length=4;
022.    var cp:Captcha = new Captcha("bt", 0, false, 20); //bt type Captcha width no noise,and fontsize=20;
023. */
024.package org.nwhy.utils{
025.    import flash.display.DisplayObjectContainer;
026.    import flash.display.Sprite;
027.    import flash.events.MouseEvent;
028.    import flash.events.Event;
029.    import flash.text.TextField;
030.    import flash.text.TextFormat;
031.    import flash.text.TextFieldAutoSize;
032.    public class Captcha extends Sprite {
033.        private var __type:String;
034.        private var __length:uint;
035.        private var __size:uint;
036.        private var __tft:TextFormat = new TextFormat();
037.        private var __noiseFlag:Boolean;
038.        private var __aryType:Array=["number","character","math","bt"];
039.        private var __aryCharacters:Array=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
040.        private var __randCode:String="";
041.  
042.        private const CHANGE:String="change";
043.        private var sp_cont:Sprite=new Sprite();
044.        private var sp_noise:Sprite=new Sprite();
045.  
046.        public function Captcha(type:String="number",length:uint=5,noiseFlag:Boolean=true,size:uint=18):void {
047.            __type = type;
048.            __length = length;
049.            __noiseFlag = noiseFlag;
050.            __size = size;
051.  
052.            __tft.font = "Arial";
053.            __tft.size=__size;
054.            __tft.bold = true;
055.            sp_cont.x=__size/2;
056.            addChild(sp_cont);
057.            if (__aryType.indexOf(__type)==-1) {
058.                __type=__aryType[3];
059.            }
060.            reRandCaptcha(null);
061.            addEventListener(MouseEvent.MOUSE_DOWN,reRandCaptcha);
062.        }
063.        private function reRandCaptcha(_e:MouseEvent){
064.            removeAllChild(sp_cont);
065.            randCaptcha(__type);
066.            drawNoise();
067.            drawBorder();
068.            dispatchEvent(new Event(CHANGE));
069.        }
070.        private function randCaptcha(type:String):void {
071.            __randCode="";
072.            switch (type) {
073.                case __aryType[0] :
074.                    randCaptchaWithNumber();
075.                    break;
076.                case __aryType[1] :
077.                    randCaptchaWithCharacter();
078.                    break;
079.                case __aryType[2] :
080.                    randCaptchaWithMath();
081.                    break;
082.                case __aryType[3] :
083.                    randCaptchaWithBt();
084.                    break;
085.                default:
086.                    randCaptchaWithNumber();
087.            }
088.        }
089.        private function randCaptchaWithNumber():void {
090.            for (var i:uint=0; i<__length; i++) {
091.                var tf:TextField=new TextField();
092.                tf.text = String(randInt(0,9));
093.                __randCode+=tf.text;
094.                tf.selectable = false;
095.                tf.x = int(__tft.size) * i;
096.                tf.textColor = randInt(0,0x888888);
097.                tf.autoSize = TextFieldAutoSize.LEFT;
098.                tf.setTextFormat(__tft);
099.                sp_cont.addChild(tf);
100.            }
101.        }
102.        private function randCaptchaWithCharacter():void {
103.            for (var i:uint=0; i<__length; i++) {
104.                var tf:TextField=new TextField();
105.                tf.text = __aryCharacters[randInt(0,51)];
106.                __randCode+=tf.text;
107.                tf.selectable = false;
108.                tf.x = int(__tft.size) * i;
109.                tf.textColor = randInt(0,0x888888);
110.                tf.autoSize = TextFieldAutoSize.LEFT;
111.                tf.setTextFormat(__tft);
112.                sp_cont.addChild(tf);
113.            }
114.            __randCode = __randCode.toLowerCase();
115.        }
116.        private function randCaptchaWithMath():void {
117.            var rand1:uint=randInt(0,5);
118.            var rand2:uint=randInt(0,10);
119.            var rand3:uint=randInt(0,5);
120.            var rand4:uint=randInt(0,10);
121.            var tf:TextField=new TextField();
122.            tf.text = "(" + rand1 + "+" + rand2 + ")" + "*(" + rand3 + "+" + rand4 + ")";
123.            tf.selectable = false;
124.            tf.textColor = randInt(0,0x888888);
125.            tf.autoSize = TextFieldAutoSize.LEFT;
126.            tf.setTextFormat(__tft);
127.            sp_cont.addChild(tf);
128.            __randCode = String((rand1+rand2)*(rand3+rand4));
129.        }
130.        private function randCaptchaWithBt():void {
131.            var rand1:Number = Number(randNumber(0,Math.PI).toFixed(2));
132.            var rand2:Number = Number(randNumber(0,Math.PI).toFixed(2));
133.            var tf:TextField = new TextField();
134.            tf.text = "Math.pow(" + rand1 + ",Math.log(" + rand2 + "))";
135.            tf.selectable = false;
136.            tf.textColor = randInt(0,0x888888);
137.            tf.autoSize = TextFieldAutoSize.LEFT;
138.            tf.setTextFormat(__tft);
139.            sp_cont.addChild(tf);
140.            __randCode = Math.pow(rand1,Math.log(rand2)).toFixed(3);
141.        }
142.        private function drawNoise():void{
143.            if(__noiseFlag){
144.                sp_noise.graphics.clear();
145.                for(var i=0;i&lt;25;i++){
146.                    var ptx = randInt(1,width);
147.                    var pty = randInt(1,height);
148.                    var ex=ptx+randInt(-width,width);
149.                    var ey=pty+randInt(-height,height);
150.                    ex=(ex<=1)?1:((ex>=(width))?(width):ex);
151.                    ey=(ey< =1)?1:((ey>=height-1)?height-1:ey);
152.                    sp_noise.graphics.lineStyle(1,randInt(0,0xFFFFFF),0.25);
153.                    sp_noise.graphics.moveTo(ptx,pty);
154.                    sp_noise.graphics.lineTo(ex,ey);
155.                }
156.                sp_noise.width= sp_cont.width + __size;
157.                sp_noise.height= sp_cont.height;
158.                addChild(sp_noise);
159.            }
160.            return;
161.        }
162.        private function drawBorder():void{
163.            graphics.clear();
164.            graphics.lineStyle(1,0x006699);
165.            graphics.drawRect(0,0,sp_cont.width+__size,height);
166.            graphics.beginFill(0);
167.        }
168.        private function removeAllChild(container:DisplayObjectContainer){
169.            while(container.numChildren>0){
170.                container.removeChildAt(0);
171.            }
172.        }
173.        private function randInt(min:int,max:int):int {
174.            return Math.random() * (max - min) + min;
175.        }
176.        private function randNumber(min:Number,max:Number):Number {
177.            return Math.random() * (max - min) + min;
178.        }
179.        public function get captcha():String{
180.            return __randCode;
181.        }
182.    }
183.}

文档说明的已经很清楚了,这里就不再介绍咯。