stergosz
stergosz

Reputation: 5860

codeigniter set_rules allow more characters

I am building a registration system and i have come to a problem.

I want to allow dots(.) into my username but i cant find a way to do this...

This is just an example which i may want to use in order features of my app as well.

This is what i got so far:

    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean');

Thanks

Upvotes: 0

Views: 3751

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220136

You'll have to create your own custom function in your controller:

class Form extends CI_Controller
{

    function index()
    {    
        $this->load->library('form_validation');

        // 'callback_valid_username' will call your controller method `valid_username`
        $this->form_validation->set_rules('username', 'Username', 'trim|required|callback_valid_username|min_length[6]|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
            // Validation failed
        }
        else
        {
            // Validation passed
        }
    }

    function valid_username($str)
    {
        if ( ! preg_match('/^[a-zA-Z0-9.]*$/', $str) )
        {
            // Set the error message:
            $this->form_validation->set_message('valid_username', 'The %s field should contain only letters, numbers or periods');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

For more information on custom validation function, read the docs:

http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

Upvotes: 3

Related Questions