Here 2 validations are added first one is if user not entering any values it will alert message.second is if user enter ‘mydoubts.in’ in the username field it will throw alert message.
step1:Create a database in mysql using phpmydmin.After installling the xaamp/Waamp Type the below url into the browser.
http://localhost/phpmyadmin/
# create a database called testlogin.
# create table users inside the the db testlogin.for creating table users you can use the following queries
1 2 3 4 5 6 |
CREATE TABLE IF NOT EXISTS `users` ( `id` tinyint(4) NOT NULL AUTO_INCREMENT, `username` varchar(10) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) |
step2:Change the values inside the database.php file inside the config folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'testlogin'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; |
stept3:create view file inside the application/views-uservalidationpage.php
uservalidationpage.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <head> </head> <body> <?php echo form_open('usernamevalidation/validateme', array('id'=>'form1')); ?> <?php echo validation_errors(); ?> <h5>User Name</h5> <input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" /> <div><input type="submit" value="Submit" /></div> </form> </body> </html> |
step4:create a model file inside the application/model-username.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php Class Username extends CI_Model { function checkusername($username) { $this -> db -> select('id, username, password'); $this -> db -> from('users'); $this -> db -> where('username', $username); $this -> db -> limit(1); $query = $this -> db -> get(); if($query -> num_rows() == 1) { return $query->result(); } else { return false; } } } ?> |
step5: create a controller inside the application/controllers/usernamevalidation.php
usernamevalidation.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Usernamevalidation extends CI_Controller{ function __construct() { parent::__construct(); $this->load->model('username','',TRUE); } function index() { $this->load->view('uservalidationpage'); } function validateme() { $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Username', 'callback_username_check'); if ($this->form_validation->run() == FALSE) { $this->load->view('uservalidationpage'); } else { //here you can add the data savings into the db. } } public function username_check($str) { $this->form_validation->set_rules('name', 'User Name', 'required'); $username = $this->input->post('name'); $result = $this->username->checkusername($username); if ($this->form_validation->run() == FALSE) { return FALSE; } else if ($result) { $this->form_validation->set_message('username_check', 'Sorry Username already exists in the database"'); return FALSE; } else { return TRUE; } } } ?> |