Form validation rules for regex_match Form validation rules for regex_match php php

Form validation rules for regex_match


First, you need to fix your pattern. Your pattern right now is a-z0-9

It needs to have a delimiter enclosing it, so something like /a-z0-9/ (If you have issues in Codeigniter using backslash delimiters, you have a number of other options: see The manual page on PCRE delimiters)

Now you have a pattern that literally matches a string with a-z0-9 someplace in it. Note that if you were trying to match that as a character class, you would need to again encase it in brackets to make it that, so /[a-z0-9]/

Now I'm going to assume that you actually wanted to check that the entire string was (lowercase) alphanumeric, not just that it had a character matching that. In which case you need a front anchor, your character class to match, a repetition modifier (I'll assume you don't want to match an empty string and make it one or more), and then an anchor to specify that the match must extend to the end:

/^[a-z0-9]+$/

You could even set a minimum length by substituting braces for the plus and providing a minimum, maximum (leave the max out to allow it to be any length over min)... for example /^[a-z0-9]{3,}$

So your third parameter would end up looking like:

'trim|required|xss_clean|regex_match[/^[a-z0-9]+$/]'

Except that this actually won't work in codeigniter apparently (as of March 2011 this was the case anyway), due to how it handles brackets inside the validation rules, so you'll need to use a callback (see here).

Using a callback:

You would place a function like the following in your controller (or of otherwise appropriate scope):

public function _usernameRegex($userName) {  if (preg_match('/^[a-z0-9]+$/', $userName ) )   {    return TRUE;  }   else   {    return FALSE;  }}

You then set the rule to call your function:

set_rules('username', 'Username', 'trim|required|xss_clean|callback__usernameRegex');

Notes: the function is named with a leading underscore to prevent url access if you create your callback function within the controller. There is an alternative way to do this by creating a class which extends the CI_Form_validation class, but that seemed like a more complicated solution. If you name the function with a leading underscore, remember that you must then include that in your rules, which results in callback__usernameRegex having two underscores total: one for the prefix of callback_ and the second for the name of the function, _usernameRegex. This may be utterly straightforward and not need pointing out to you, but to me it seemed like something that could easily be missed. See the linked answer for Adding custom callback to Codeigniter Form Validation for a detailed explanation on extending CI_Form_validation. Note that if you extend the form validation, you do not need to append your function with callback_ when you reference it in set_rules.

You can also set the message for your new callback:

$this->form_validation->set_message('usernameRegex', 'The %s field must only contain lowercase letters and/or numbers');


The alternative route is to modify Codeigniter's own pattern match used on the form_validation rules. The link to Ellislab's forum describes the issue and then a potential way to work around it further down the thread. From glancing at the Codeigniter code and the fix specified, the proposed solution they provided may break using arrays in rules: a better regex to replace the one in the Codeigniter function by using recursive matching would solve both, but might run into performance issues on heavier rules.

Codeigniter's pattern matching for form_validation rules is found in its system/libraries/Form_validation.php file, on line 115 as of 2.1.3.


Use following code its working fine at my end

$this->form_validation->set_rules('username', 'Username','trim|required|callback_username_check|regex_match[/^[a-z0-9]+$/]');


php's preg_match wants your regular expression surrounded by a non alpha-numeric character (usually everyone uses /), change your setRules() to this:

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|regex_match[/a-z0-9/]');