Unable to set validation rule for alphabetical characters in codeigniter -
i using codeigniter 3, , referred various stackoverflow links having similar problem mines. not able solve issue. callback function customalpha called file->my_form_validation.php, located in libraries folder of project. changed regular expression several times,but no use. please help.
controller->login_c.php
<?php class login_c extends ci_controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); $this->load->library('encrypt'); $this->load->model('user/login_m'); $this->load->library('session'); $this->load->library('form_validation'); } public function register_user() { $this->form_validation->set_rules( 'uname', 'full name', 'trim|xss_clean|required|min_length[4]|callback_customalpha', array( 'required' => 'please provide %s.', 'customalpha' => 'only characters allowed in full name field' ) ); $this->form_validation->set_rules('upass', 'password', 'required|min_length[5]'); $this->form_validation->set_rules('cpass', 'password confirmation', 'required|matches[upass]'); $this->form_validation->set_rules('uemail', 'email', 'required|valid_email|is_unique[user.uemail]'); $this->form_validation->set_rules('umobile', 'mobile number', 'required|min_length[10]|max_length[10]'); if($this->input->post('oemail')) { $this->form_validation->set_rules('oemail', 'email', 'required|valid_email|is_unique[user.oemail]'); $oemail = $this->input->post('oemail'); } if ($this->form_validation->run() == false) { $errors['err'] = validation_errors(); $this->load->view('user/signup.html',$errors); } else { //code store user entered data in database } } } ?>
my_form_validation.php
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class my_form_validation extends ci_form_validation { public function __construct($rules = array()) { parent::__construct($rules); } public function customalpha($str) { //validation alphabetical characters , spaces if ( !preg_match('/^(?:[a-za-z]+(?:\s[a-za-z]+|$)){1}$/', $str) ) { return false; } else { return true; } } } ?>
i able solve issue on own. below solution worked.
my_form_validation.php
class my_form_validation extends ci_form_validation { function __construct($config = array()) { parent::__construct($config); } public function customalpha($str) { if ( !preg_match('/^([a-z]+(-| )?)+$/', $str) ) { return false; } else { return true; } } }
login_c.php
class login_c extends ci_controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); $this->load->library('encrypt'); $this->load->model('user/login_m'); $this->load->library('session'); $this->load->library('form_validation'); } public function register_user() { $this->form_validation->set_rules( 'uname', 'full name', 'required|min_length[4]', array( 'required' => 'please provide %s.' ) ); $this->form_validation->set_rules('upass', 'password', 'required|min_length[5]'); $this->form_validation->set_rules('cpass', 'password confirmation', 'required|matches[upass]'); $this->form_validation->set_rules('uemail', 'email', 'required|valid_email|is_unique[user.uemail]'); $this->form_validation->set_rules('umobile', 'mobile number', 'required|min_length[10]|max_length[10]'); $validation = $this->form_validation->customalpha($this->input->post('uname')); if($this->input->post('oemail')) { $this->form_validation->set_rules('oemail', 'email', 'required|valid_email|is_unique[user.oemail]'); $oemail = $this->input->post('oemail'); } else if ($this->form_validation->run()== false) { $errors['err'] = validation_errors(); $this->load->view('user/signup.html',$errors); } else if($validation == false) { $errors['err'] = "error in name field"; $this->load->view('user/signup.html',$errors); } else { //code store user entered data in database } } } ?>
Comments
Post a Comment