1

I m validating a data by clicking the submit button and then again loading the views. I want just to show the errors on the page before loading the controller. Its is not a form validation. it is just a data validiation.

3
  • 1
    What do you means by data ? Commented Feb 2, 2017 at 12:27
  • data validation ? Commented Feb 2, 2017 at 12:33
  • 1
    means for example i m checking an array that whether it have a duplicate values or not. If not then put values to the database else return a error. Commented Feb 2, 2017 at 13:06

2 Answers 2

2

I think you can do the validation using AJAX.

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

Comments

0
in view page

    <script type="text/javascript">
    $(document).ready(function() {
        /// make loader hidden in start
    $('#loading').hide();
     $('#email').blur(function(){
        var email_val = $("#email").val();
        var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
        if(filter.test(email_val)){
            // show loader
            $('#loading').show();
            $.post("<?php echo site_url()?>/user/email_check", {
                email: email_val
            }, function(response){
                $('#loading').hide();
                $('#message').html('').html(response.message).show().delay(4000).fadeOut();
            });
            return false;
        }
    });

    });  
</script>

in controller function 

    public function email_check()
    {
        // allow only Ajax request    
        if($this->input->is_ajax_request()) {
        // grab the email value from the post variable.
        $email = $this->input->post('email');
        // check in database - table name : tbl_users  , Field name in the table : email
        if(!$this->form_validation->is_unique($email, 'tbl_users.email')) {
        // set the json object as output                 
         $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'The email is already taken, choose another one')));
            }
        }
    }

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.