Jump to content

My validation method is running on valid and invalid...


exceedinglife

Recommended Posts

$("#btnRegister").click(function (e) {
        e.preventDefault();

        if (validateRegistration()) {
            calltest();

function validateRegistration() {
    var email = $("#txtEmail").val();
    var password = $("#txtPassword").val();
    var password2 = $("#txtPassword2").val();
    var isValid = false;
    var alertText = "";
    var genericAlert = '<div class="alert alert-danger alert-dismissible fade show" role="alert"><button type="button" ' +
        'class="close" data-dismiss="alert" aria-label="Close" aria-hidden="true">&times;</button>';

    // EMAIL VERIFICATION
    if (email != "") {
        var emailRegex = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
        if (email.match(emailRegex)) {
            isValid = true;
        } else {
            alertText = "Email is not in the correct format.";
            emailFormatAlert = genericAlert + alertText + '</div>';
            $("#alertArea").append(emailFormatAlert);
            isValid = false;
        }
    } else {    //alert("Email cannot be empty.");     + '</div>'
        alertText = "Email cannot be empty.";
        emailAlert = genericAlert + alertText + '</div>';
        $("#alertArea").append(emailAlert);
        isValid = false;
    }
    // PASSWORD VERIFICATION
    if (password != "") {
        if (password.length < 5) {
            $("#alertPass").show();
            isValid = false;
        } else {
            isValid = true;
        }
    } else {
        alertText = "Password cannot be empty.";
        passwordAlert = genericAlert + alertText + '</div>';
        $("#alertArea").append(passwordAlert);
        isValid = false;
    }
    // PASSWORD MATCH
    if (password != password2) {
        alertText = "Passwords do not match!";
        pass2Alert = genericAlert + alertText + '</div>';
        $("#alertArea").append(pass2Alert);
        isValid = false;
    } else {
        isValid = true;
    }

    return isValid;
}

Hello all,

My $.ajax() is running even when my validateRegistration() is invalid. My validateRegistration() is registering true wether its valid or invalid. Could someone look at this method and let me know if they see anything wrong with it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.