tags:

views:

18

answers:

2

I can’t figure out what is missing so that when e-mail is valid it will skip the last invalid message and move to next item on form for validation:

enter code here 
    if (document.form1.email.value.length > 0) {

    var tst = document.form1.email.value;
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

    var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
    var aLst = emailRE.exec(tst)
    if (!aLst) {
        alert(tst + ' is not a valid e-mail')
    } else {
        var sLst = aLst[1].toLowerCase()
        for (var i = 0; i < okd.length; i++) {
            if (sLst == okd[i]) {
                //    alert(aLst[1] + ' is allowed');-->

           }
       }

            if (i == okd.length) alert(aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.')

            document.form1.email.select();
            return false;


    }   
}
A: 

I'd recommend placing this code into a function, maybe named ValidateEmail().

In your loop: if you've determined the email is valid, return true;. This will prevent further execution. If that domain doesn't match, have it continue looping to check the others.

If the loop completes without returning true, you'll know it didn't match anything so return false; at the very end.

EDIT: Use try/catch instead:

if (document.form1.email.value.length > 0) {

    var tst = document.form1.email.value;
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

    try {
        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
        var aLst = emailRE.exec(tst)

        if (!aLst)
            throw (tst + ' is not a valid e-mail');

        // isValidDomain will be changed to 'true' only if it matches an item in the array
        var isValidDomain = false;

        var sLst = aLst[1].toLowerCase()
        for (var i = 0; i < okd.length; i++) {
            if (sLst == okd[i]) {
                isValidDomain = true;
                // We break here because a match has been found - no need to compare against the other domain names.
                break;
            }
        }

        if(!isValidDomain)
            throw (aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.');

        // If execution reaches here, you know it passed both tests!
        return true;

    }
    catch(err) {

        // This code block runs whenever an error occurs

        alert(err);
        document.form1.email.select();
        return false;
    }
}

throw basically acts like a goto command. It will jump directly to the catch(err) portion of the code.

More info about try, catch, and throw:

Colin O'Dell
Also, in your first `if` statement, you can `return false;` is the email definitely isn't valid. This will prevent checking each domain.
Colin O'Dell
Not working, apparently I don't know how to do it...There has to be a GoTo command or something to let it jump the last if when already true...why is this so tough?
miweiser
I don't need a loop, or a break, I just need it to go to next step and skip the last
miweiser
I've got a much better/clearer solution for you. Please see my revised answer for the code example.
Colin O'Dell
Thank You! Part was my fault - I think it is comparing the entire e-mail to the domain half past "@"...?
miweiser
OK, that seems to work except that again the "Break" stops the entire process from continuing to the next form element which in this case is username. I took break out originally because of this issue.
miweiser
A: 

Thank you very much Colin!

I had to remove the following 2 lines to avoid halting the code from running on to next validation field:

              isValidDomain = true;
                    // We break here because a match has been found - no need to compare against the other domain names. 
                    // break - exits code from running on down to next item on page
                }
            }

            if (!isValidDomain)
                throw (aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.');

            // If execution reaches here, you know it passed both tests! 
         //   return true; - was not needed, stops code from running on page

        }
        catch (err) {
miweiser
Sweet, I'm glad you got it working :)
Colin O'Dell