CodeIgniter jQuery验证(检查电子邮件是否存在)

时间:2015-08-03 17:39:02

标签: php jquery codeigniter validation

我在这里搜索了stackoverflow和一般的网站,但在尝试应用每个修复后,它似乎不起作用(验证没有出现在我的表单中)。我正在使用此库(jqueryvalidation.org)作为我的表单,并使用连接到控制器的远程功能,该控制器链接到包含数据库检查的模型。

如果有任何不清楚的地方,请参阅下面的代码并回复:)

函数控制器(email_exists方法) - /application/controllers/Functions.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Functions extends CI_Controller {
    public function index() {
        show_404();
    }

    public function email_exists() {
        if (isset($_POST['email'])) {
            $this->load->model('account_model');

            if ($this->account_model->email_exists($this->input->post('email')) == TRUE) {
                echo 'TRUE';
            } else {
                echo 'FALSE';
            }
        }
    }
}

帐户模型(email_exists方法) - /application/models/Account_model.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Account_model extends CI_Model {
    public function email_exists($email) {
        $query = $this->db->select('email_address')->get_where('accounts', array('email_address' => $email));

        if ($query->num_rows() == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

jQuery验证规则(部分)

rules: {
    'email_address': {
        required: true,
        email: true,
        remote: {
            url: "/functions/email_exists",
            type: "post",
            data: {
                email: function() {
                    return $("#email").val();
                }
            }
        }
    }

表单输入(电子邮件部分 - 文件的一部分):

$input_email_address = array('name' => 'email_address',
                             'type' => 'email',
                             'id' => 'email_address',
                             'class' => 'form-control',
                             'value' => $this->input->post('email_address'),
                             'autofocus' => 'autofocus');
echo form_input($input_email_address);

任何帮助都会非常感激,我从早上开始玩它但没有运气。提前谢谢你:)

2 个答案:

答案 0 :(得分:1)

选择器与输入字段不同。

#email!= #email_address

rules: {
    'email_address': {
        required: true,
        email: true,
        remote: {
            url: "http://YOUR_URL",
            type: "post",
            data: {
                email: function() {
                    return $("#email_address").val();
                }
            }
        }
    }

答案 1 :(得分:0)

我最好说使用jQuery.validator.addMethod('isEmailExists'....

这样的规则

email: { required: true, email: true, isEmailExists: true },