I am trying to validate some user form data for the login portion of my website, however I am running into some issues, where even when I am returning false, the action for the given form still happens.
Here is where I use some jQuery to handle the form submission:
$(function() {
$("#submit_login").submit(function() {
var email = $(".log_user").val();
var elength = email.length;
var pass = $(".log_pass").val();
var plength = pass.length;
var checkemail = email.match(/^[a-zA-z0-9]+@.+\..+$/);
var cmlength = checkemail.length;
var checkpass = pass.match(/^([a-zA-z0-9]|[_\-!])*$/);
var cplength = checkpass.length;
if(cmlength!=elength) {
//email is invalid
alert("email is bad");
}
if(cplength!=plength) {
//password is invalid
alert("password is bad");
}
event.preventDefault();
});
});
What I am trying to do is use a regular expression to match with the email and password to determine whether or not what they entered is a valid user and pass, and if it is, I will then make a request to the server to actually check if the user exists, however if I were to enter an invalid email, nothing happens and it still sends that request to the server and redirects to my login handler.
Once I get this figured out I am going to replace those alerts with just a simple toggleClass and make a red outline along with a prompt telling the user what is wrong with their input, but I can't get there without figuring out whats wrong with this first!
Here is my form in HTML
<form class="login_form" method="GET" action="handlers/login_handler.php">
<div class="user"><span>Email: </span><input class="log_user" type="text" name="email" id="username"></div>
<div class="pass"><span>Password: </span><input class="log_pass" type="text" name="password" id="password"></div>
<div class="submit_box">
<div class="create_box">
<a id="create_account" href="create.php">Create account</a>
</div>
<button id="submit_login" type="submit" value="Submit">Login</button>
</div>
</form>
Any help would be appreciated!