Question

I have a form that I want to validate before submission.

I have tried different solutions from stackoverflow but haven't got the following code to work.

I can't get the form to submit when everything is ok.

$('#form1').submit(function(e) {
    e.preventDefault();
    var xxx = $('#xxx').val();
    var yyy = $('#yyy').val();
    var zzz = $('#zzz').val();
    var uuu = $('#uuu').val();
    var data_string = 'uuu=' + uuu;
    if (zzz != '000000' && zzz != '') {
        $.ajax({
            url: 'ajax.php',
            type:'POST',
            data: data_string,
            dataType: 'json',
            cache: false,
            success: function(response){
                if (response == false) {
                    if (xxxl == '') {
                        alert("Text!");
                    }
                    else if (yyy == '') {
                        alert("Text!");
                    }
                    else {
                        $(this).trigger('submit');
                    }
                }
            }
        });
    }
    else {
        $(this).trigger('submit');
    }
});
Was it helpful?

Solution

OK I finally solved this myself. I made 2 different functions. 1 for submit and the other for ajax. Here's an example:

             $('#form1').submit(function(event) {
                var xxx = $('#xxx').val();
                var yyy = $('#yyy').val();
                var zzz = $('#zzz').val();
                var uuu = $('#uuu').val();
                if (zzz != '000000' && zzz != '') {
                    validate_brutokaal(uuu, function(response) {
                        if (response === false) {
                            if (xxx == '') {
                                alert("Text!");
                                event.preventDefault ? event.preventDefault() : event.returnValue = false;
                            }
                            else if (yyy == '') {
                                alert("Text!");
                                event.preventDefault ? event.preventDefault() : event.returnValue = false;
                            }
                        }
                        else {
                            return true
                        }
                    });
                }
                else {
                    return true;
                }
            });

            function validate_xxxyyy(uuu, callback) {                   
                var data_string = 'uuu=' + uuu;
                $.ajax({
                    url: 'ajax.php',
                    type:'POST',
                    data: data_string,
                    dataType: 'json',
                    cache: false,
                    async: false,
                    success: function(response){
                        callback(response);
                    }
                });
            }

OTHER TIPS

Bind jQuery submit function to FORM element like

$("#formId").submit(function(){
....
return true;
});

logically return true or false .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top