OAFC_Rob Posted November 5, 2011 Share Posted November 5, 2011 Hi I have a contact form that when submitted should preform some jQuery to check for errors, if errors > 0 then retun false ie don't submit to the POST variable. Then if the POST is true some PHP is done to ensure some validation is taking place. However, the jQuery isn't returning false and allows the POST, can anyone see what I have done wrong or missed. Cheers $(document).ready(function() { var errors = 0; //REMAINING COMMENTS $('#comments').keypress(function() { if($("#comments").val().length>0) { $("#commentsError").html(""); } var maxChars = 1000; var charsEntered = $('#comments').val().length; var charsRemaining = maxChars - charsEntered; $('#charsRemaining').html("("+charsRemaining+" Characters remaining)"); }); $("#contactForm").submit(function() { var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if($("#name").val().length == 0) { $("#nameErrors").html(" * Name required2"); errors=errors+1; alert(errors+" 1"); } else { $("#nameErrors").html(""); } if($("#contactNo").val().length == 0) { $("#contactNumErrors").html(" * Contact number required2"); errors=errors+1; alert(errors+" 2"); } else { $("#contactNumErrors").html(""); } if($("#email").val().length == 0) { $("#emailErrors").html(" * E–mail required2"); errors=errors+1; alert(errors+" 3"); } else if(emailPattern.test($("#email").val()) == false || emailPattern.test($("#email2").val()) == false) { $("#emailErrors").html(" * Invalid e-mail address2"); errors=errors+1; alert(errors+" 4"); } else if($("#email").val()!= $("#email2").val()) { $("#emailErrors").html(" * E–mail addresses do not match2"); errors=errors+1; alert(errors+" 5"); } else { $("#emailErrors").html(""); } if($("#comments").val().length == 0) { $("#commentsErrors").html(" * Please tell supply some comments2"); errors=errors+1; alert(errors+" 6"); } if(errors>0) { $(".error").css("display","block"); alert(errors+" Total"); return false; } }); }); Quote Link to comment Share on other sites More sharing options...
OAFC_Rob Posted November 5, 2011 Author Share Posted November 5, 2011 I have found a work around, get the validation to run on the button click and if errors event.preventDefault(); If anyone can explain why the first bit wouldn't work that would be brilliant. Quote Link to comment Share on other sites More sharing options...
OAFC_Rob Posted November 5, 2011 Author Share Posted November 5, 2011 Doesn't matter i've figured it all out now, the event.preventDefault(); wasn't need the return false needed to go before the css display block and I needed an else part to the statement to submit the form if no errors Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.