codignitor3 validation with custom password
//user registration function
public function registrationProcess()
{
if(checkUsersession())
{
redirect('user');
}
$data['title'] = "Registration";
$sitesetting = $this->my_model->getData('tblsetting',$resultType = 'row_array',$arg=[]);
$data['countries'] = $this->my_model->getData('tblcountries',$resultType = 'result_array',$arg=[]);
$data['sitename'] = $sitesetting['sitename'];
$data['sitelogo'] = $sitesetting['logo'];
$data['sitefavicon'] = $sitesetting['favicon'];
$data['metakeyword'] = $sitesetting['metakeyword'];
$data['contactphone'] = $sitesetting['contactphone'];
$data['contactemail'] = $sitesetting['contactemail'];
$data['logo'] = $sitesetting['logo'];
$data['contactaddress'] = $sitesetting['contactaddress'];
$data['metadescription'] = $sitesetting['metadescription'];
$data['footercopyright'] = $sitesetting['footercopyright'];
$data['facebooklink'] = $sitesetting['facebooklink'];
$data['twitterlink'] = $sitesetting['twitterlink'];
$data['linkedinlink'] = $sitesetting['linkedinlink'];
$data['googlelink'] = $sitesetting['googlelink'];
$data['pinterestlink'] = $sitesetting['pinterestlink'];
$data['blogs'] = $this->my_model->getData('tblblog',$resultType = 'result_array',$arg=['select'=>['id','title','image'],'order'=>['col'=>'id','type'=>'DESC'],'limit'=>['upto'=>'5']]);
$data['footercharity'] = $this->my_model->getData('tblcharity',$resultType = 'result_array',$arg=['select'=>['id','name','image'],'order'=>['col'=>'id','type'=>'DESC'],'limit'=>['upto'=>'3']]);
$data['footerblog'] = $this->my_model->getData('tblblog',$resultType = 'result_array',$arg=['select'=>['id','title','image'],'order'=>['col'=>'id','type'=>'DESC'],'limit'=>['upto'=>'3']]);
if (isset($_POST['g-recaptcha-response']))
{
$secret = '6Le2q64ZAAAAAKSgsvzNMLLTi5g770Umcvd-NUfm';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$this->form_validation->set_rules('firstname', 'First Name','trim|required');
$this->form_validation->set_rules('lastname', 'Last Name','trim|required');
$this->form_validation->set_rules('email', 'Email','trim|required|is_unique[tbluser.email]');
$this->form_validation->set_rules('phone', 'Phone Number','trim|regex_match[/^[+]?\d+$/]');
$this->form_validation->set_rules('address', 'Address','trim');
$this->form_validation->set_rules('country', 'Country','trim|required');
$this->form_validation->set_rules('city', 'City','trim');
$this->form_validation->set_rules('zipcode', 'Zip Code','trim');
$this->form_validation->set_rules('state', 'State','trim');
$this->form_validation->set_rules('password','Password','trim|required|alpha_numeric|callback_valid_password');
$this->form_validation->set_rules('repassword', 'Retype Password','trim|required|matches[password]|alpha_numeric');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('user/register',$data);
}
else
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$return = sendMailfunction($email);
if($return)
{
$insertiondata = [
'email' => $this->input->post('email'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'zip-code' => $this->input->post('zipcode'),
'state' => $this->input->post('state'),
'password' => base64_encode($this->input->post('password')),
'isverified' => '0',
'isblocked' => '1',
'created' => date('Y:m:d'),
];
if(!$this->my_model->insertData('tbluser',$insertiondata))
{
$this->session->set_flashdata('insertionsuccess','Your account is created. A verification link has been sent to your email. Please check your inbox or spam folder.');
redirect('user/register');
}
else
{
$this->session->set_flashdata('insertionerror','Something went wrong! try again');
$this->load->view('user/register',$data);
}
}
else
{
$this->session->set_flashdata('insertionerror','Something went wrong ! try again');
$this->load->view('user/register',$data);
}
}
}
else
{
$data['capchtaerror'] = "Please verify yourself ! you are not a robot";
$this->load->view('user/register',$data);
}
}
else
{
$data['capchtaerror'] = "Please verify yourself ! you are not a robot";
$this->load->view('user/register',$data);
}
}
//custom password function
public function valid_password($password = '')
{
$password = trim($password);
$regex_lowercase = '/[a-z]/';
$regex_uppercase = '/[A-Z]/';
$regex_number = '/[0-9]/';
$regex_special = '/[!@#$%^&*()\-_=+{};:,<.>§~]/';
if (empty($password))
{
$this->form_validation->set_message('valid_password', 'The {field} field is required.');
return FALSE;
}
if (preg_match_all($regex_lowercase, $password) < 1)
{
$this->form_validation->set_message('valid_password', 'The {field} field must be at least one lowercase letter.');
return FALSE;
}
if (preg_match_all($regex_uppercase, $password) < 1)
{
$this->form_validation->set_message('valid_password', 'The {field} field must be at least one uppercase letter.');
return FALSE;
}
if (preg_match_all($regex_number, $password) < 1)
{
$this->form_validation->set_message('valid_password', 'The {field} field must have at least one number.');
return FALSE;
}
// if (preg_match_all($regex_special, $password) < 1)
// {
// $this->form_validation->set_message('valid_password', 'The {field} field must have at least one special character.' . ' ' . htmlentities('!@#$%^&*()\-_=+{};:,<.>§~'));
// return FALSE;
// }
if (strlen($password) < 8)
{
$this->form_validation->set_message('valid_password', 'The {field} field must be at least 8 characters in length.');
return FALSE;
}
if (strlen($password) > 32)
{
$this->form_validation->set_message('valid_password', 'The {field} field cannot exceed 32 characters in length.');
return FALSE;
}
return TRUE;
}
Comments
Post a Comment