2017-02-02 2 views
2

送信ボタンをクリックしてビューを再度読み込んでデータを検証します。コントローラをロードする前に、ページにエラーを表示したいだけです。それはフォームの検証ではありません。これは単なるデータ検証です。Codeigniter:コントローラに行く前にデータを検証する

+1

データではどういう意味ですか? – rahulsm

+0

データ検証? –

+1

は、配列が重複値を持つかどうかをチェックすることを意味します。そうでない場合は、データベースに値を入力します。それ以外の場合はエラーを返します。 –

答えて

2

私はあなたがAJAXを使用して検証を行うことができると思います。

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_-][email protected][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'))); 
      } 
     } 
    } 
関連する問題