在提交之前检查电子邮件值

时间:2013-10-04 10:55:46

标签: actionscript-3 flash-cs6

在提交表单之前,我需要一个功能来检查@.符号。

负责检查值是否已插入的函数:

// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {

    // validate fields
    if(!name_txt.length) {
        status_txt.text = "Please enter your name";
    } else if (!email_txt.length) {
        status_txt.text = "Please enter your e-mail address";
    } else if (!phone_txt.length) {
        status_txt.text = "Please enter your phone number";
    } else {

        // All is good, send the data now to PHP

        // ready the variables in our form for sending
        variables.userName = name_txt.text;
        variables.userEmail = email_txt.text;       
        variables.userPhone = phone_txt.text;
        variables.userShop = shopList.value;

        // Send the data to PHP now
        varLoader.load(varSend);

    } // close else condition for error handling

} // close validate and send function

我尝试创建一个单独的功能来检查电子邮件符号:

// Checking e-mail
function checkEmail():Boolean {

    var reEmail:String = email_txt.text;
    var emailOk:Boolean = false;
    var checkArray1:Array = reEmail.split("@");
    if (checkArray1.length >1) {

        var checkArray2:Array = checkArray1[1].split(".");
        if (checkArray2.length >1) {

            emailOk = true;
        }
    }
    return emailOk;
}

但这不起作用。你会如何实现这一目标?

更新:我已尝试在ValidateAndSend功能中运行该功能。但现在如果电子邮件地址错误,它将无法发送邮件,但仍会显示成功的提交邮件。

// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {

// validate fields
if(!name_txt.length) {
    status_txt.text = "Please enter your name";
} else if (!email_txt.length) {
    status_txt.text = "Please enter your e-mail";

    // Checking e-mail
    function checkEmail():Boolean {

    var reEmail:String = email_txt.text;
    var emailOk:Boolean = false;
    var checkArray1:Array = reEmail.split("@");
    if (checkArray1.length >1) {

        status_txt.text = "Please check your e-mail address";

        var checkArray2:Array = checkArray1[1].split(".");
        if (checkArray2.length >1) {

            emailOk = true;
        }
    }
    return emailOk;
    }

} else if (!phone_txt.length) {
    status_txt.text = "Please enter your phone number";
} else {

    // All is good, send the data now to PHP

    // ready the variables in our form for sending
    variables.userName = name_txt.text;
    variables.userEmail = email_txt.text;       
    variables.userPhone = phone_txt.text;
    variables.userShop = shopList.value;

    // Send the data to PHP now
    varLoader.load(varSend);

} // close else condition for error handling

} // close validate and send function

1 个答案:

答案 0 :(得分:1)

你应该使用正则表达式。

您的checkEmail()函数不再需要

// function ValidateAndSend
function ValidateAndSend (event:MouseEvent):void {
    var emailCheckRegExp:RegExp = /^[\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    // validate fields
    if(name_txt.length == 0) {
        status_txt.text = "Please enter your name";
    } 
    else if (email_txt.length == 0) {
        status_txt.text = "Please enter your e-mail address";
    } 
    else if (phone_txt.length == 0) {
        status_txt.text = "Please enter your phone number";
    }
    else if(emailCheckRegExp.exec(email_txt.text) == null)
    {
        status_txt.text = "Entered e-mail is not valid";
    }
    else {

        // All is good, send the data now to PHP

        // ready the variables in our form for sending
        variables.userName = name_txt.text;
        variables.userEmail = email_txt.text;       
        variables.userPhone = phone_txt.text;
        variables.userShop = shopList.value;

        // Send the data to PHP now
        varLoader.load(varSend);

    } // close else condition for error handling

} // close validate and send function