This is very simple library which generates Javascript code for forms validation
It generates validation rules and custom error messages for jQuery Validation Plugin
- Load library in your controller.
$this->load->library('jquery_validation');
- Set CodeIgniter standard validation rules ( The same rules format which has form_validtion library ).
$rules = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|min_length[2]'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required|valid_email'
),
array(
'field' => 'url',
'label' => 'URL',
'rules' => 'required'
)
);
- Set error messages.
$messages = array(
'username' => array( 'required' => "Username is required",
'min_length' => "Please enter more then 2 char"
),
'email' => array( 'required' => "Email is required",
'valid_email' => "Please enter valid email"
)
);
- Apply validation rules and messages to library.
$this->jquery_validation->set_rules($rules);
$this->jquery_validation->set_messages($messages);
- Generate Javascript validation code.
// pass css selector for your form to run method
$validation_script = $this->jquery_validation->run('#registration_form');
// echo $validation_script in your <script> tag
- Enjoy :-)