0

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?

2
  • why are you doing this? if you are validating using Ajax/Jquery use client site validation. Commented Dec 8, 2014 at 4:27
  • @Vinie you know, because I use ajax, it does not refresh the page, so there is no difference to validate from server side or client side. What is your suggestion for client side validation? And what if a person change client codes form inspect element in browser? Thanks a lot Commented Dec 8, 2014 at 8:45

1 Answer 1

1

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

                        }                  

                });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.