Is there a simple way to block words going into a text box?
For example in the username text box we do not want a member using the domain name or anything similar.
Is there a simple way to block words going into a text box?
For example in the username text box we do not want a member using the domain name or anything similar.
Yes, you can block specific words or patterns from being entered into a textbox in CodeIgniter 3 by utilizing form validation and custom callback functions. Here’s how you can do it:
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username');
check_username()
. This function will check if the provided username contains the words you want to block:public function check_username($str) {
$blocked_words = array('domainname', 'anotherblockedword'); // Add your blocked words to this array
foreach ($blocked_words as $word) {
if (stripos($str, $word) !== false) {
$this->form_validation->set_message('check_username', 'The {field} field cannot contain the word "' . $word . '".');
return false;
}
}
return true;
}
if ($this->form_validation->run() == FALSE) {
// Validation failed, reload the form and show errors
$this->load->view('your_form_view');
} else {
// Validation passed, process the form
// ...
}
your_form_view
), ensure you display form validation errors:echo validation_errors();
Now, when a user tries to submit a username containing one of the blocked words, the form validation will fail, and they will be alerted with an appropriate error message.
Thank you so much. I do have validation so will need to study your script.
Im now in the process of registering the domain name & in a few days expect the site to be online although in a very basic format. If you give me your email address the site will invite you to join.
OK, but I thought you might like to be part of the website. Excuse me for being so wrong.
I do have another problem where I cant get the username & password loaded into a 2nd database table;
And later I will need an expert on fingerprinting. Are you familiar with Python?
Since the post was marked as solved
I will not reply to that post.
I don’t know if this is allowed on this site, but you posed a problem, so let’s try it out.
Let’s see …
$this->db->update('tbl_member', $data);
to insert the user details into the table. This is incorrect since the update
function is used to update existing records. Instead, you should be using the insert
function for inserting new records.$this->input->post()
. If the post array is empty, then the form hasn’t been submitted. If it has data, the form has been submitted.user_id
is likely an auto-incremented column, and date_reg
would probably need a timestamp when the user is registered. You should include this in your insert method.So, let’s rewrite the necessary parts of your code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class RegisterUser extends CI_Controller {
public function index() {
$this->load->library('session');
$this->load->helper(array('form', 'url', 'file'));
$this->load->model('Username_model', 'username_model');
// Check if the form has been submitted
if (!$this->input->post()) {
$this->load->view('registeruser');
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->username_model->insert_user($username, $password);
// Redirect or load a success view after inserting data.
redirect('success_page'); // Example redirect
}
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class Username_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
public function insert_user($username, $password) {
$data = array(
'user_name' => $username,
'pass_word' => $password,
'date_reg' => date('Y-m-d H:i:s') // Assuming date_reg is a datetime field.
);
$this->db->insert('tbl_member', $data);
}
}
Once you’ve made these changes and made sure your database server is running correctly, try registering a user. If the problem persists, check CodeIgniter’s logs and your database server logs for more specific error messages.
Yes, I’m familiar with Python and am available where necessary. ([email protected])
Ive sent you an email.