Vahid Najafi
Vahid Najafi

Reputation: 5243

Codeigniter email validation with jquery ajax

I have a registration form that I've written by codeigniter framework. I use this code for email validation:

$this->form_validation->set_rules('email', 'email', 'valid_email|required');

While I haven't used jquery ajax, it was returning errors for invalid email address. But now with this ajax code, It accepts any input in email input.

<script type="text/javascript">
    $('#add_staff input[type="submit"]').on('click', function() {
        var form_data = {
            name: $('#name').val(),
            username: $('#username').val(),
            password: $('#password').val(),
            passconf: $('#passconf').val(),
            email: $('#email').val(),
            modir: $('#modir').val(),
            ajax: '1'

        };

        $.ajax({
            url : "<?php echo site_url('admin/add_user_validation'); ?>" ,
            type: 'POST',
            data: form_data ,
            success: function(msg) {
                $('body').html(msg);
            }
        });
           return false;    
    });
</script>

How can I solve this problem?

Upvotes: 0

Views: 415

Answers (1)

Vinie
Vinie

Reputation: 2993

on validation error on php page send following response

echo json_encode(array(array('status'=>false,'message'=>  validation_errors())));

and in jquery response

success: function(msg) {
$.each(eval(msg),function(i,item)
               {
                   if(item.status)
                   {
                       $("#result").empty().append(item.message); //on success

                    }else{
                         $("#result").empty().append(item.message); // on validation error

                        }                  

                });
}

Upvotes: 1

Related Questions