Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
JavaScript
$("#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">×</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;
}


What I have tried:

I tried testing my php and it works if the validation is valid or if it is invalid. I want it to ONLY run if it is VALID. I tried a e.preventDefault() to stop it if it is invalid but still I have not any luck.
Posted
Updated 21-Jan-19 17:18pm
Comments
Bryian Tan 21-Jan-19 22:59pm    
did you paste the code correctly?? look like there are some syntax errors from the first and fourth lines, missing "}"
TheBigBearNow 21-Jan-19 23:15pm    
I did not copy all the code I know i didnt copy the '}'
I am only worried about the validation function

1 solution

OK. assuming the posted code was not complete. There are couple of thing that I see.

1. There something wrong with the regular expression, the backslash need to be escaped (adding \) Regex Tutorial - Literal Characters and Special Characters[^] . So it should look like
[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

2. I think the last else statement is redundant, because it overwrite the isValid indicator in the scenario where both password are empty, it will return true.

3. Here how your code look like after the minor modification. CP_validateregistrati - JSFiddle[^]
 
Share this answer
 
Comments
TheBigBearNow 22-Jan-19 22:15pm    
Thank you so much for finding that mistake! I would not have caught that. BUT My function is still returning VALID even when it is INVALID... I am stepping through it and the boolean invalid = false happens so I dont understand why on the if(validregister()) it returns TRUE ?
TheBigBearNow 22-Jan-19 22:27pm    
What i needed to do what return false on each validation instead of isvalid = false because it was overwriting it at the end.
Bryian Tan 22-Jan-19 23:10pm    
have you try the example on JSFiddle?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900