Jump to content

jQuery Form Submitting when is return false :-s


OAFC_Rob

Recommended Posts

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;
                    
                }
                
            });
    });

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.