Blog Details

Form Validation


                                
                                    <h2>* <span style="text-decoration: underline;">Form Validation :-</span></h2>
let's describe the ideal scenario:
<ol>
	<li>A form is <strong>displayed.</strong></li>
	<li>You <strong>fill</strong> it in and <strong>submit it</strong>.</li>
	<li>If you submitted <strong>something invalid</strong>, or perhaps missed a required item, the form is redisplayed containing your data along with an <strong>error message</strong> describing the problem.</li>
	<li>This process <strong>continues</strong> until you have <strong>submitted a valid form</strong>.</li>
</ol>
&nbsp;
<h2>* List of validation rules :-</h2>
<ul>
	<li>required</li>
	<li>matches[field_name]</li>
	<li>min_length[x]</li>
	<li>max_length[x]</li>
	<li>exact_length[x]</li>
	<li>alpha</li>
	<li>alpha_numeric</li>
	<li>alpha_dash</li>
	<li>numeric</li>
	<li>integer</li>
	<li>is_natural</li>
	<li>is_natural_no_zero</li>
	<li>valid_email</li>
	<li>valid_emails</li>
	<li>valid_ip</li>
	<li>valid_base64</li>
</ul>
&nbsp;
<h2><strong>* Creating Validation Application at CodeIgniter:-</strong></h2>
<h3>1) The Form :-</h3>
Using a text editor, create a form called <strong><dfn>myform.php</dfn></strong>. In it, place this code and save it to your <strong></strong>
<h3><samp>applications/views/folder:</samp></h3>
&lt;html&gt;

&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo $this-&gt;validation-&gt;error_string; ?&gt;

&lt;?php echo form_open('form'); ?&gt;

&lt;h5&gt;Username&lt;/h5&gt;
&lt;input type="text" name="username" value="" size="50" /&gt;

&lt;h5&gt;Password&lt;/h5&gt;
&lt;input type="text" name="password" value="" size="50" /&gt;

&lt;h5&gt;Password Confirm&lt;/h5&gt;
&lt;input type="text" name="passconf" value="" size="50" /&gt;

&lt;h5&gt;Email Address&lt;/h5&gt;
&lt;input type="text" name="email" value="" size="50" /&gt;

&lt;div&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/div&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

&nbsp;
<h3>2) The Controller :-</h3>
Using a text editor, create a controller called <dfn>form.php</dfn>. In it, place this code and save it to your <strong></strong>
<h3><samp>applications/controllers/folder:</samp></h3>
&lt;?php

class Form extends Controller {

function index()
{
$this-&gt;load-&gt;helper(array('form', 'url'));

$this-&gt;load-&gt;library('validation');

<strong>$rules['username'] = "required|min_length[5]|max_length[12]";
$rules['password'] = "required|matches[passconf]";
$rules['passconf'] = "required";
$rules['email'] = "required|valid_email";</strong>

$this-&gt;validation-&gt;set_rules($rules);

if ($this-&gt;validation-&gt;run() == FALSE)
{
$this-&gt;load-&gt;view('myform');
}
else
{
$this-&gt;load-&gt;view('formsuccess');
}
}
}
?&gt;

&nbsp;
<h3>3) The Success Page :-</h3>
Using a text editor, create a form called <dfn><strong>formsuccess.php</strong></dfn><strong>.</strong> In it, place this code and save it to your <strong></strong>
<h3><samp>applications/views/folder:</samp></h3>
<strong></strong> <em id="__mceDel">&lt;html&gt;</em>

<em id="__mceDel"> &lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;</em>

&lt;h3&gt;Your form was successfully submitted!&lt;/h3&gt;

&lt;p&gt;&lt;?php echo anchor('form', 'Try it again!'); ?&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;

&nbsp;

&nbsp;
                                    


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