AnsweredAssumed Answered

Optional Javascript Email Validation

Question asked by Josh Whitlow on Sep 8, 2015
Latest reply on Sep 8, 2015 by Josh Whitlow
Sugar 6.5.20

If I hit "create" under my module, no validation occurs yet and I can save it with no data entered in that field.

If I type some numbers and letters, it disables the save button and pops up the alert message. So that works. If I type in a proper email format, it re-enables the save button. Good.

My problem is that if I erase all the text, then the save button won't re-enable, and it keeps popping up with the alert message still.

What's wrong with my code?

SUGAR.util.doWhen("typeof($('#assistant_email_c'))!= 'undefined'",     function() {
    YAHOO.util.Event.addListener(YAHOO.util.Dom.get("assistant_email_c"), 'change', validateEmail);
    }
);                                                                     
function validateEmail() {if (/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/.test(assistant_email_c.value) || $("#assistant_email_c").value =="undefined") {
    //Initialize button before calling
    $("#SAVE_FOOTER").button().button("enable");
    return (true);
    }
    else {
    alert("You have entered an invalid email address!");
    //Initialize button before calling
    $("#SAVE_FOOTER").button().button("disable");
    return (false); 
    }
}

Outcomes