mardi 27 octobre 2020

Javascript else if statement stops executing unexpectedly

This is my first question to the community here so please forgive any shortcomings with respect to best practices.

I have a JavaScript else if statement which stops executing immediately after the following snippet of the else if statement:

} else if (email != '') {
    if (validateEmail(email)) {
        email = email.toLowerCase();
    }
}

I have tried removing the nested if statement completely and logging a message to the console to make sure that when the condition of the else if statement is being met I can very in the console. For example, the output to the console in this case is "email has a value"

} else if (email != '') {
        console.log('email has a value');
    }

After that the else if statement seems to stop executing. I'm really hung up here and any guidance is greatly appreciated - thank you in advance.

Following is the comprehensive set of pertinent code if that is helpful to provide some context:

function applyFocus(target, message) {
    alert(message);
    document.getElementById(target).focus();
}

function validateEmail(mail) {
    var mailLower = mail.toLowerCase();
    if (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(mailLower)) {
        console.log('returning true');
        return true;
    } else {
        errorMessage = 'Please enter a valid email address.';
        applyFocus('email', errorMessage);
        console.log('returning false');
        return false;
    }

if (firstName == '') {
    // do something
 } else if (lastName == '') {
    // do something
 } else if (email == '') {
    // do something
 } else if (email != '') {
    if (validateEmail(email)) {
        email = email.toLowerCase();
    }
 } else if (industry == '') {
    // do something
 } else {
     // do something
 }

Aucun commentaire:

Enregistrer un commentaire