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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.