Form validation in Codeigniter 3.0

It's only fair to share...Share on Facebook0Share on Google+7Tweet about this on Twitter0Share on LinkedIn0Pin on Pinterest0Digg thisShare on StumbleUpon0

form-validation
Due to lots of spamming and hacking we already recommend server side validation in PHP. We can implements form validation in Codeigniter 3.0 very easily using “form_validation” library.

So first we have to include “form_validation” library:

Using set_rules function we will set the validation rules.

The above methods take four parameters as input:

  1. Field name – same as form field name.
  2. Name to identify the field name in meaningful manner e.y field name is user_name then for identify we user ‘Username’.
  3. Validation rules for the form fields.
  4.  Set custom error messages on any rules given for current field. (optional Parameter)

Now taking an example of user name. if username field can not be blank then we use

now, if username can not be blank and user input will be minimum 6 and max 15 character then

If we have to check unique user_name from database table (tbl_users) then

Note: is_unique rule requires Query Builder to be enabled in order to work.

Controller File: Home.php


View File: home.php

 

Rule References: Here are some listing of the rules with their specification

Rule Parameter Description Example
required No Returns FALSE if the form element is empty.
matches Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
regex_match Yes Returns FALSE if the form element does not match the regular expression. regex_match[/regex/]
differs Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the
parameter. Note: This rule requires Query Builder to be
enabled in order to work.
is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter than the parameter value. min_length[3]
max_length Yes Returns FALSE if the form element is longer than the parameter value. max_length[12]
exact_length Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]
greater_than Yes Returns FALSE if the form element is less than or equal to the parameter value or not
numeric.
greater_than[8]
greater_than_equal_to Yes Returns FALSE if the form element is less than the parameter value,
or not numeric.
greater_than_equal_to[8]
less_than Yes Returns FALSE if the form element is greater than or equal to the parameter value or
not numeric.
less_than[8]
less_than_equal_to Yes Returns FALSE if the form element is greater than the parameter value,
or not numeric.
less_than_equal_to[8]
in_list Yes Returns FALSE if the form element is not within a predetermined list. in_list[red,blue,green]
alpha No Returns FALSE if the form element contains anything other than alphabetical characters.
alpha_numeric No Returns FALSE if the form element contains anything other than alpha-numeric characters.
alpha_numeric_spaces No Returns FALSE if the form element contains anything other than alpha-numeric characters
or spaces. Should be used after trim to avoid spaces at the beginning or end.
alpha_dash No Returns FALSE if the form element contains anything other than alpha-numeric characters,
underscores or dashes.
numeric No Returns FALSE if the form element contains anything other than numeric characters.
integer No Returns FALSE if the form element contains anything other than an integer.
decimal No Returns FALSE if the form element contains anything other than a decimal number.
is_natural No Returns FALSE if the form element contains anything other than a natural number:
0, 1, 2, 3, etc.
is_natural_no_zero No Returns FALSE if the form element contains anything other than a natural
number, but not zero: 1, 2, 3, etc.
valid_url No Returns FALSE if the form element does not contain a valid URL.
valid_email No Returns FALSE if the form element does not contain a valid email address.
valid_emails No Returns FALSE if any value provided in a comma separated list is not a valid email.
valid_ip No Returns FALSE if the supplied IP is not valid.
Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.
valid_base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters.

Comments

  1. Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">