4

I have this regular expression for validation in javascript:

/^(?:'[A-z](([\._\-][A-z0-9])|[A-z0-9])*[a-z0-9_]*')$/

Now I want the same regular expression for the form validation using Codeigniter's form validation:

$this->form_validation->set_rules('username', 'Nombre de usuario', 'required|min_length[2]|max_length[15]|regex_match[/^[A-Z a-z 0-9 _ . \-]+$/]|is_unique[user.username]');

the regex in that line is not equivalent to the one I mentioned.

When trying to copy and paste same regex, It doesn't work. I know this is dumb i just can't seem to fully understand regular expressions.

Limon
  • 1,772
  • 7
  • 32
  • 61
  • 2
    **Warning:** `[A-z]` is not the same as `[A-Za-z]`. It also matches several punctuation characters whose code points lie between `Z` and `a`. It is an error to use the range `A-z` in a character class. – Alan Moore Mar 11 '14 at 19:54

2 Answers2

6

Though there is no regex_match() method in CodeIgniter validation library, It's not listed in the CI User Guide.

Per @Limon's comment:

There is a bug in CodeIgniter with the pipe |, it breaks the regex.

CodeIgniter uses | as a separator between validate methods.

Therefore, to prevent from breaking the regex, you could create a callback method in your Controller to validate the input by matching the regex:

public function regex_check($str)
{
    if (preg_match("/^(?:'[A-Za-z](([\._\-][A-Za-z0-9])|[A-Za-z0-9])*[a-z0-9_]*')$/", $str))
    {
        $this->form_validation->set_message('regex_check', 'The %s field is not valid!');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

Then add the validation rule, as follows:

$this->form_validation->set_rules('username', 'Nombre de usuario', 'required|min_length[2]|max_length[15]|callback_regex_check|is_unique[user.username]');
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • hi, thanks for the reply. There is a regex_match in codeigniter's form_validation.php. It's this: public function regex_match($str, $regex) { if ( ! preg_match($regex, $str)) { return FALSE; } return TRUE; } – Limon Jan 29 '14 at 19:27
  • Thanks again. I have found in this question http://stackoverflow.com/questions/13804721/regex-match-in-codeigniter-form-validation-generates-message-preg-match-no that there is a bug in Codeigniter with the pipe, it breaks the regex. Is there a way to make my regex without the pipe | ? – Limon Jan 29 '14 at 19:34
  • @Limon Well, CodeIgniter use `|` for separating the validator methods, The only option I could suggest is using callback method. – Hashem Qolami Jan 29 '14 at 19:36
  • Yes it's a solution. But I also think callbacks have to be implemented for more complex functions. In this case I consider there isn't such a complexity so another equivalent regular expression would be better – Limon Jan 29 '14 at 19:39
  • @Limon Yes, if the regex could be rewritten in different way. – Hashem Qolami Jan 29 '14 at 19:41
0

You might provide rules as an array :

$this->form_validation->set_rules('username', 'Nombre de usuario', array('required', 'min_length[2]', 'max_length[15]', 'regex_match[/^(?:\'[A-z](([\._\-][A-z0-9])|[A-z0-9])*[a-z0-9_]*\')$/]', 'is_unique[user.username]'));

This way regex with pipes won't crash

bfav
  • 151
  • 1
  • 4