0

good day!

I have a php form. I have made some ajax validation on it but everytime i click the submit button it does not validate, and when I try to issue an alert command on ajax function, it returns undefined on certain variables?

Here is my present code:
<Form codes>

               <form method="POST" id="newsletter-form" name="newsletter-form" onSubmit="return doSubmit();">
                    <p>
                         <label for="name">Your name</label>
                         <input type="text" name="name" id="name" value="" tabindex="1" />
                    </p>
                    <p>
                         <label for="email">Your email address</label>
                         <input type="text" name="email" id="email" value="" tabindex="2" />
                    </p>
                    <p>
                        We hate spammers!<br />
                        <input type="submit" value="Join Us" />
                    </p>
                </form>

<Ajax validation codes>

function doSubmit(){    
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var name  = $('newsletter-form [name="name"]').val();  
        var email_address = $('newsletter-form [name="email"]').val();

        if(name == ""){
            alert("Please enter your name.");
            return false;
        }

        if(email_address == ""){
            alert("Your email is required.");
            return false;
        }
};

Thanks for helping..!

2
Contributors
2
Replies
18
Views
2 Years
Discussion Span
Last Post by blocker
0

Missing hash on the front of your form's ID

function doSubmit(){    
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var name  = $('#newsletter-form [name="name"]').val();  
        var email_address = $('#newsletter-form [name="email"]').val();
        if(name == ""){
            alert("Please enter your name.");
            return false;
        }
        if(email_address == ""){
            alert("Your email is required.");
            return false;
        }
}

Also, if I can make a suggestion for a cleaner method

$("#newsletter-form").submit(function(){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var name  = $('#newsletter-form [name="name"]').val();
    var email_address = $('#newsletter-form [name="email"]').val();

    if(name == ""){
        alert("Please enter your name.");
        return false;
    }
    if(email_address == ""){
        alert("Your email is required.");
        return false;
    }
});

Using this, you can remove the onSubmit="return doSubmit();" from your form tag.

0

thank you pixelsoul!

I've made a slight modification and it works! Code below!

$(document).ready(function() {
    $('#submit-form').click(function(){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var name = $('#newsletter-form [name="name"]').val();
    var email_address = $('#newsletter-form [name="email"]').val();

    if(name == ""){
    alert("Please enter your name.");
    return false;
    }

    if(email_address == ""){
    alert("Your email is required.");
    return false;
    }else if(reg.test(email_address) == false){
    alert("Invalid Email Address.");
    return false;
    }
    });
});
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.