Search code examples
actionscript-3flash-cs6

Check for e-mail values before submitting


Before submitting the form I need a function to check for @ and . symbols.

Function responsible for checking that values are inserted:

// 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

I've tried creating a separate function for checking the e-mail symbols:

// 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;
}

but this doesn't work. How would you achieve this?

Update: I've tried running the function inside ValidateAndSend function. But now if the email address is wrong it won't send the message but it still displays a successful submitting message.

// 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

Solution

  • You should use regular expressions for this.

    Your checkEmail() function won't be needed

    // 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