How to block words going into text box in codeigniter 3

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:

  1. Set up form validation: In your controller, load the form validation library and set up your validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username');

  1. Define the callback function : In the same controller, define your callback function 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;
}

  1. Handle the form submission : Before processing the form, run the validation:
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
    // ...
}

  1. Display form validation errors : In your view (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.

@bensan, Am always here if you need me.
Good luck

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 …

  1. Database Update Issue:
  • You are using $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.
  1. Controller and Logic Issue:
  • The logic to determine whether to load the view or process the form isn’t clear in your controller. Typically, a form submission in CodeIgniter is identified using $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.
  1. Database Connection:
  • If you’re getting errors and can’t open phpMyAdmin, that is a server/database issue. You’ll need to fix that to proceed. Rebooting can help. You can also check the server logs to see if there’s any clue about the error.
  1. Database Table Columns:
  • Remember that 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:

Controller:

<?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
        }
    }
}

Model:

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.

Sponsor our Newsletter | Privacy Policy | Terms of Service