Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get submit() to work

Tags:

jquery

When ever I try submitting the form I either get an error, or if I type it manually in my console I get this error:

RangeError: Maximum call stack size exceeded

Here's my code:

$(document).ready(function() {
    $('#contact').on('submit', function(event) {
        event.preventDefault();
        var valid = 1;
        var name = $('input[name$="name"]');
        var email = $('input[name$="email"]')
        var phone = $('input[name$="phone"]');
        var comment = $('input[name$="comment"]');
        if (!name.val() && valid == 1) {
            valid = 0;
            alert('Please fill out the Name field');
            name.focus();
        }
        if (!email.val() && valid == 1) {
            valid = 0;
            alert('Please fill out the E-mail Address field');
            email.focus();
        }
        if (!phone.val() && valid == 1) {
            valid = 0;
            alert('Please fill out the Phone field');
            phone.focus();
        }
        if (valid == 1) {
            $('#contact').submit();
        }
    })
});
like image 831
user990717 Avatar asked Dec 04 '22 02:12

user990717


1 Answers

You're calling submit from the submit handler, so it's infinite recursion. You want to return true if valid and false otherwise.

Edit: Oh, and I'm not sure you want to preventDefault in this case.

like image 59
Nogwater Avatar answered Jan 05 '23 16:01

Nogwater