0

I creating submit form with ajax but, i cant validate form before submit..

iam using codeigniter 3 with jquery validate

my code working, but i need set form like min lenghth, email format etc

this my code before (with input class="required")

    $(document).ready(function(){  
        $(".save_post").click(function(){

                 if ($("#post_val").valid()) {
                        $("#post_val").submit();
                     }else{
                         return false;
                     }

                var data = $('.post_form').serialize();
                $.ajax({
                        type: 'POST',
                        url: "<?= base_url() ?>admin_ajx/post_ajx/update_post",
                        data: data,
                        success: function(data) {                                                                             

                            alert("Post Success!");


                        }                                    
                    });
                });
            });

i trying to modified that code to this, but code its not working

   $(document).ready(function(){  
        $(".save_post").click(function(){

                    $('#post_val').validate({ 
                            rules: {
                                title: {
                                    required: true,
                                    minlength: 10
                                },
                                image: {
                                    required: true,
                                    minlength: 5
                                }
                            },
                            messages: {
                                title:{
                                    required: 'title error',
                                    minlength: 'kurang dari 10'
                                },
                                image: {
                                    required: true,
                                    minlength: 5
                                }
                            }
                        });

                var data = $('.post_form').serialize();
                $.ajax({
                        type: 'POST',
                        url: "<?= base_url() ?>admin_ajx/post_ajx/update_post",
                        data: data,
                        success: function(data) {                                                                             

                            alert("Post Success!");


                        }                                    
                    });
                });
            });

my form like this

        <form action="<?= base_url() ?>admin/add-post" method="POST" role="form" class="post_form" id="post_val">
            <input type="text" name="title" class="form-control" placeholder="Title" id="post_title" onkeyup="auto_alias();">
            <input class="form-control input-sm required" type="text" name="image" id="img_url" readonly="readonly" onclick="openKCFinder(this)"  style="cursor:pointer" />         
            <a class="btn btn-sm btn-info save_post" onClick="CKupdate();$('#article-form').ajaxSubmit();">POST</a>           
        </form> 

i expect, i can validate that form before submit

dmforaname
  • 63
  • 1
  • 10

1 Answers1

0

You are using jquery validate in an incorrect manner.

For starters you don't nest validate in a click event, and then you don't run your submit code independently. Instead you run it in the submit callback of validate.

A sample looks like this:

$("#ajax-contact-form").validate({
        errorClass: "text-danger",
        rules: {
            name: {
                required: true,
            },
            phone: {
                required: false,
                digits: true,
                minlength: 7,
            },
            email: {
                required: true,
                email: true,
            },
            message: {
                required: true,
                minlength: 10,
            },
        },
        submitHandler: function (form, e) {
            e.preventDefault(); // important to prevent issues!!!
            $("#contact-button").html('Sending...');
            var str = $(form).serialize(); // to get your serialized form
            $.ajax({
                type: "POST",
                url: base_path + "/contact/send",
                data: str,
                dataType: 'json',
                success: function (data) {
                    var result = '';
                    if (data.status === 'success') {
                        $(form).trigger('reset');
                        result = '<div class="alert alert-success"><i class="fa fa-check"></i> ' + data.msg + '</div>';
                    } else {
                        result = '<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + data.msg + '</div>';
                    }
                    $("#contact-button").html('Send Message');
                    $('#note').html(result);
                    setTimeout(function () {
                        $("#note").fadeOut();
                    }, 8000);
                }
            });
        },
    });

also I would remove $('#article-form').ajaxSubmit(); from this onClick="CKupdate();$('#article-form').ajaxSubmit();". It holds no reference to current code, and it won't work with validate.

Alex
  • 9,215
  • 8
  • 39
  • 82