Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

php - Form validation rules for regex_match

I'm trying to figure out what I'm doing wrong with this validation rule because its saying this error.

Validation rule:

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

Error:

Severity: Warning
Message: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...