Saturday, June 22, 2013

Simple Validation rule in CodeIgniter




now, i can share about validation rule in CodeIgniter. if you using validation default in CodeIgniter, you must follow rule module Library, Helper, etc in CodeIgniter.

if you not expert using JavaScript, ajax, or jQuery for validation in CodeIgniter you can try using validation in CodeIgniter this is rule validation in CodeIgniter :

Rule Reference

The following is a list of all the native rules that are available to use:
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]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter then the parameter value. min_length[6]
max_length Yes Returns FALSE if the form element is longer then 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 the parameter value or not numeric. greater_than[8]
less_than Yes Returns FALSE if the form element is greater than the parameter value or not numeric. less_than[8]
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_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 Yes Returns FALSE if the form element is not exactly the parameter value.
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_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.

 
if you confused for using rule, you can read manual book or you can change and custom rule validation in class module.
for modify rule validation in CodeIgniter, you can open file in system/library/form_validation.php

for example in rule alpha..

public function alpha($str)
{
        return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
}

and using rule :

$this->form_validation->set_rules('name','Name Message','alpha');

alpha only make validation for if you input in form using space rule alpha make not valid you input form. because space not alphabetical characters. if you want using rule alpha and make space is valid in alpha rule, you just modify function alpha :

public function alpha($str)
{
        return ( ! preg_match("/^([a-z ])+$/i", $str)) ? FALSE : TRUE;
}

i just add space in [a-z] to [a-z ] very simple.
not modify? just make a new function in class form_validation.

finish, and sorry if my english not good.


No comments:

Post a Comment