exceedinglife Posted January 22, 2019 Share Posted January 22, 2019 $("#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; } 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 More sharing options...
requinix Posted January 22, 2019 Share Posted January 22, 2019 What will your function return if the email address is empty, the password is 4 characters long, and the second password matches the first? Link to comment Share on other sites More sharing options...
exceedinglife Posted January 23, 2019 Author Share Posted January 23, 2019 It always returns TRUE I'm not sure why I stepped through the JS and the validregister() returns isvalid = false so why on the if(validreg()) it returns true... I have if(validreg()){alert("valid");}else{alert("Invalid");} Its always valid Link to comment Share on other sites More sharing options...
exceedinglife Posted January 23, 2019 Author Share Posted January 23, 2019 I now have it solved what I needed to do what return false on each validation instead of having isvalid = false because it was overwriting it at the end. Link to comment Share on other sites More sharing options...
Barand Posted January 23, 2019 Share Posted January 23, 2019 function validate() isValid = true; if email is invalid set isValid = false if password is invalid set isValid = false if passwords do not match set isValid = false return isValid end Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.