在AS3中按下按钮之前验证文本字段

时间:2012-12-13 08:00:11

标签: actionscript-3 flash validation event-handling

这是as3:

import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.events.Event;
import flash.display.MovieClip;


wheeler.guide.visible = false;
center.addEventListener(MouseEvent.CLICK, rotateHorizontal);

Stop_mc.addEventListener(MouseEvent.CLICK, stopspin);

function rotateHorizontal(e:MouseEvent):void
{
    stage.addEventListener(Event.ENTER_FRAME,spinner);

}

var mcbtn:MovieClip;
var prizes:Array = new Array(0,180,0,40,30,0,80,75,0,10,0,25,120,0);
var prizewon:int;

for (var i:int=0; i<14; ++i)
{
    mcbtn = wheeler["btn" + (i + 1)];

    if (! mcbtn || ! prizes[i])
    {
        continue;
    }

    mcbtn.txt.text = "Earn " + String(prizes[i]) + " messages";
}

function spinner(e:Event)
{
    wheeler.rotationZ +=  15;
    pointer_txt.text = " ";

    if (wheeler.rotationZ > 360)
    {
        wheeler.rotationZ = wheeler.rotationZ - 360;
    }
    //trace(wheeler.rotationZ);
}

function completed(event:Event):void
{
    if (event.target.data.success == "true")
    {
        trace("Completed");
    }
}

function stopspin(e:Event):void
{
    var txt:String;
    stage.removeEventListener(Event.ENTER_FRAME,spinner);
    prizewon = -1;
    prizewon = (14 - Math.floor(wheeler.rotationZ / (360 / 14)));

    if (prizes[prizewon - 1])
    {
        txt = "You have earned " + prizes[prizewon - 1] + " Messages";
    }
    else
    {
        txt = "You have earned no Messages";
    }

    pointer_txt.text = txt;

    var phpVar:URLVariables = new URLVariables();
    phpVar.code_txt = code_txt.text;
    phpVar.name_txt = name_txt.text;
    phpVar.phone = phone_txt.text;
    phpVar.Result = pointer_txt.text;
    var phpLoader:URLLoader = new URLLoader(phpFileRequest);
    var phpFileRequest:URLRequest = new

 URLRequest("http://localhost/spin_webapp/deposit.php");
    phpFileRequest.method = URLRequestMethod.POST;
    phpFileRequest.data = phpVar;
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

    phpLoader.addEventListener(Event.COMPLETE,completed);
    phpLoader.load(phpFileRequest);
}

1 个答案:

答案 0 :(得分:0)

您需要创建一个事件来处理文本字段上的按键。

每当按键发生时,按键事件处理函数将检查文本字段是否为NULL或执行其他验证。如果一切正常,则设置name_txt__valid=TRUE之类的标记。

然后,如果name_txt__valid, phone_txt__validpointer_txt__valid全部为TRUE,则将动画片段按钮设为可见,否则将其隐藏。

显然,必须告知用户文本字段是/有效的,否则他们不会意识到按钮消失的原因。


<强>更新

抱歉,我现在无法测试我的下面的代码,但下面应该更好地阐述我认为需要做的事情。

在初始化期间创建侦听器的语法: 例如

name_txt.addEventListener(Event.CHANGE,name_txt_ChangeListener);
//Also lets initialize our flags...
name_txt__valid=false;
phone_txt__valid=false;
pointer_txt__valid=false;
center.enabled = false;

参考:AddchangeeventtoTextField

然后只需添加验证逻辑:

private function name_txt_ChangeListener (e:Event):void {
...
  trace("Text field event triggered");
  //Check if text is null or invalid
  //If valid then set a flag like name_txt__valid=TRUE
  if (name_txt.text == "") {
     name_txt__valid=false;      
  } else {
     name_txt__valid=true;      
  }

  //Then make a function call to decide whether to show/hide the center button.
  showHideCenterButton();
...
}

function showHideCenterButton(){
...
  //If  name_txt__valid, phone_txt__valid and pointer_txt__valid, 
  // all are TRUE then enable the button. Else disable it like center.enabled = false;
  if( (name_txt__valid==true) &&
      (phone_txt__valid==true) &&
      (pointer_txt__valid==true)
    ) {
       center.enabled = true;
    } else {
       center.enabled = false;
    }
...
}