In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Captcha in Flash

Small Captcha completly in as3
Get Adobe Flash player
by lehrersohn 07 Jul 2011
    Embed
/**
 * Copyright lehrersohn ( http://wonderfl.net/user/lehrersohn )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7LsD
 */

package {
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.text.TextFieldAutoSize;
    
    import flash.display.*;
    import flash.events.*;
    
    import flash.ui.Keyboard;
    import flash.ui.Mouse;
        
    import flash.geom.Rectangle;
    
    public class Captcha extends Sprite
    {
        private var captchawidth:Number = 130;
        private var captchaheight:Number = 20;
        
        private var captchaText:TextField;
        private var inputText:TextField;
        private var statusField:TextField;
        
        private var captchaString:String;
        private var lineHolder:Sprite;
        
        public function Captcha():void
        {
            
            generateTextfield();
            drawbg();
            
            // INPUT //
            var myFormat2:TextFormat = new TextFormat();
            myFormat2.size = 12;
            myFormat2.font = "Verdana, Helvetica, Arial, _sans";
            inputText = new TextField();
            inputText.defaultTextFormat = myFormat2;
            inputText.multiline = false;
            inputText.wordWrap = false;
            inputText.selectable = true;
            inputText.width = captchawidth;
            inputText.height = captchaheight;
            inputText.border = true;
            inputText.type = TextFieldType.INPUT;
        
            inputText.textColor = 0x222222;
        
            inputText.embedFonts = false;
            
            inputText.x = captchawidth + 14;
            inputText.text = "";
            
            addChild(inputText);
            
            // Status //
            statusField = new TextField();
            statusField.defaultTextFormat = myFormat2;
            statusField.multiline = false;
            statusField.wordWrap = false;
            statusField.selectable = false;
            statusField.width = captchawidth;
            statusField.height = captchaheight;
            statusField.border = false;
            statusField.mouseEnabled = false;
        
            statusField.textColor = 0x222222;
        
            statusField.embedFonts = false;
            statusField.autoSize = TextFieldAutoSize.LEFT;
                        
            statusField.htmlText = "Please enter Code!";
            
            statusField.x = 4;
            statusField.y = captchaheight + 4;

            
            addChild(statusField);
            
            this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
            
        }
        
        private function drawbg():void 
        {
            lineHolder = new Sprite();
            lineHolder.alpha = 0.5;
            addChild(lineHolder);
            var rows:uint=10;
            var d:Number=12.5;
            var w:Number=(captchawidth-d)/rows-d;
            var h:Number=captchaheight;
            
            for (var i:int=0; i<4; i++)
            {
                for (var j:int=0; j<rows; j++)
                {
                    var tempStart:Number = randRange(-5, 5);
                    
                    var box_mc:Sprite=new Sprite();
                    var fillColor:uint=0xffffff*Math.random();
                    box_mc.graphics.beginFill(fillColor);
                    box_mc.graphics.drawRect(tempStart,0,w,h);
                    box_mc.graphics.endFill();
                    
                    box_mc.x=d+(w+d)*j;
                    
                    var tempRot:Number = randRange(-30, 30);
                    
                    box_mc.rotation = tempRot;
                    
                    lineHolder.addChild(box_mc);
                }
            }
        }
        
        
        private function generateRandomString(newLength:uint = 1, userAlphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String
        {
            var alphabet:Array = userAlphabet.split("");
            var alphabetLength:int = alphabet.length;
            var randomLetters:String = "";
            for (var i:uint = 0; i < newLength; i++)
            {
                randomLetters += alphabet[int(Math.floor(Math.random() * alphabetLength))];
            }
            return randomLetters;
        }
        
        private function randRange(minNum:Number, maxNum:Number):Number
        {
            return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum)
        }
        
        private function generateTextfield():void 
        {
            var myFormat:TextFormat = new TextFormat();
            myFormat.size = 14;
            myFormat.font = "Verdana, Helvetica, Arial, _sans";
            
            captchaText = new TextField();
            captchaText.defaultTextFormat = myFormat;
            captchaText.multiline = false;
            captchaText.wordWrap = false;
            captchaText.selectable = false;
            captchaText.width = captchawidth;
            captchaText.height = captchaheight;
            captchaText.border = false;
            captchaText.mouseEnabled = false;
        
            captchaText.textColor = 0x666666;
        
            captchaText.embedFonts = false;
            captchaText.autoSize = TextFieldAutoSize.CENTER;
            
            captchaString = generateRandomString(6, "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789");
            
            captchaText.htmlText = '<B>'+captchaString+'</B>';
            
            addChild(captchaText);    
            
        }
        
        private function onKeyPressed(e:KeyboardEvent):void
        {
            // Check what to do
            switch(e.keyCode)
            {
                case Keyboard.ENTER:
                    checkInput();    
                    break;                        
            }
            
        }
        
        
        public function checkInput ():void
        {
            var tempStr:String = inputText.text;
                    
            if(tempStr.toLowerCase() == captchaString.toLowerCase())
            {
                statusField.y = 0;
                statusField.htmlText = '<font color="#009900">Code accepted.</font>'
                
                // Remove everything //
                removeChild(captchaText);
                removeChild(inputText);
                
                while (lineHolder.numChildren)
                {
                    lineHolder.removeChildAt (0);
                }
            }
            else
            {    
                
                statusField.htmlText = '<font color="#ff0000">Wrong Code!</font>'
                
                reset();
            }
        }
        
        public function reset():void 
        {
            // Reset everything //
            inputText.text = "";
            removeChild(captchaText);
            
            while (lineHolder.numChildren)
            {
                lineHolder.removeChildAt (0);
            }

            generateTextfield();    
            drawbg();
        }    
    }
}