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.
stept1: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> |
step2: 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 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Usernamevalidation extends CI_Controller{ 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'); if ($this->form_validation->run() == FALSE) { return FALSE; } else if ($str == 'mydoubts.in') { $this->form_validation->set_message('username_check', 'The %s field can not be the word "mydoubts.in"'); return FALSE; } else { return TRUE; } } } ?> |