Blog Details

Form Validation



                                

* Form Validation :-

let's describe the ideal scenario:
  1. A form is displayed.
  2. You fill it in and submit it.
  3. If you submitted something invalid, or perhaps missed a required item, the form is redisplayed containing your data along with an error message describing the problem.
  4. This process continues until you have submitted a valid form.
 

* List of validation rules :-

  • required
  • matches[field_name]
  • min_length[x]
  • max_length[x]
  • exact_length[x]
  • alpha
  • alpha_numeric
  • alpha_dash
  • numeric
  • integer
  • is_natural
  • is_natural_no_zero
  • valid_email
  • valid_emails
  • valid_ip
  • valid_base64
 

* Creating Validation Application at CodeIgniter:-

1) The Form :-

Using a text editor, create a form called myform.php. In it, place this code and save it to your 

applications/views/folder:

<html> <head> <title>My Form</title> </head> <body> <?php echo $this->validation->error_string; ?> <?php echo form_open('form'); ?> <h5>Username</h5> <input type="text" name="username" value="" size="50" /> <h5>Password</h5> <input type="text" name="password" value="" size="50" /> <h5>Password Confirm</h5> <input type="text" name="passconf" value="" size="50" /> <h5>Email Address</h5> <input type="text" name="email" value="" size="50" /> <div><input type="submit" value="Submit" /></div> </form> </body> </html>  

2) The Controller :-

Using a text editor, create a controller called form.php. In it, place this code and save it to your 

applications/controllers/folder:

<?php class Form extends Controller { function index() { $this->load->helper(array('form', 'url')); $this->load->library('validation'); $rules['username'] = "required|min_length[5]|max_length[12]"; $rules['password'] = "required|matches[passconf]"; $rules['passconf'] = "required"; $rules['email'] = "required|valid_email"; $this->validation->set_rules($rules); if ($this->validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } } } ?>  

3) The Success Page :-

Using a text editor, create a form called formsuccess.php. In it, place this code and save it to your 

applications/views/folder:

 <html> <head> <title>My Form</title> </head> <body> <h3>Your form was successfully submitted!</h3> <p><?php echo anchor('form', 'Try it again!'); ?></p> </body> </html>    


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote