简体   繁体   中英

CodeIgniter jQuery Validation (check if email exists)

I have searched around here in stackoverflow and to websites in general, but after trying to apply every fix, it doesn't seem to work (validation doesn't appear in my form). I'm using this library ( jqueryvalidation.org ) for my form and using the remote feature that connects to a controller which links to the model containing the database check.

See the code below and reply back if anything is not clear :)

Functions Controller (email_exists method) - /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';
            }
        }
    }
}

Account Model (email_exists method) - /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 Validation Rules (part)

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

Form Input (email section - part of the file):

$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);

Any help would be really appreciated, I have been playing with it since the morning but no luck. Thank you in advance :)

The selecter is not the same as the input field.

#email != #email_address

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

I better say use jQuery.validator.addMethod('isEmailExists'....

And rules like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM