Welcome Guest, Not a member yet? Register   Sign In
Form validation regex_match
#1

[eluser]Perkin5[/eluser]
I want a form field to match this regex:

[a-zA-Z]\s

which I believe should match any alphabetical character or whitespace character and I am using it as follows:

Code:
$this->form_validation->set_rules('name','Name','required|regex_match[[a-zA-Z]|\s]|xss_clean');

but it does not work correctly. With a purely alphabetical string in the input field I get the error message: The Name field is not in the correct format.

What am I doing wrong?
#2

[eluser]Matalina[/eluser]
The regex string is not properly formatted

Code:
function regex_match($str, $regex)
{
  if ( ! preg_match($regex, $str))
  {
   return FALSE;
  }

  return  TRUE;
}

so I think you need to do

Code:
$this->form_validation->set_rules('name','Name','required|regex_match[/[a-zA-Z]|\s/]|xss_clean');

Tho that is only going to match the first character.
Code:
$this->form_validation->set_rules('name','Name','required|regex_match[/([a-zA-Z]|\s)+/]|xss_clean');

And if you add in extra characters it's still going to pass.
Code:
$this->form_validation->set_rules('name','Name','required|regex_match[/^([a-zA-Z]|\s)+$/]|xss_clean');

That should match only alpha characters and space.
#3

[eluser]Perkin5[/eluser]
I tried copy/pasting your suggestion into the method but I still get the same error message :-(

Also this php error is thrown


A PHP Error was encountered

Severity: Warning

Message: preg_match() [function.preg-match]: No ending delimiter '/' found

Filename: libraries/Form_validation.php

Line Number: 911

Any ideas? Strange as the delimiter is there.
#4

[eluser]Matalina[/eluser]
Try copying and pasting again. It's possible you hit my mistake I fixed a few seconds later.
#5

[eluser]Perkin5[/eluser]
No, still same problem and php error. I noticed the changes in your code but they didn't seem to do the trick.
#6

[eluser]Matalina[/eluser]
try this /^[a-zA-Z\s]+$/
#7

[eluser]CroNiX[/eluser]
The problem might be from using the pipe (|) char within the brackets for the regex_match rule. The pipe is used by CI for separating individual rules in the rules string and I'm not sure if it checks for the pipe within the passed parameter of the regex_match rule before exploding the rules string by the pipe character.

So, it might be trying to process the following individual rules:
Code:
Array
(
    [0] => required
    [1] => regex_match[/[a-zA-Z]
    [2] => \s/]
    [3] => xss_clean
)
If it is and you need to use the pipe, you might just need to just create a custom validation rule for that field.
#8

[eluser]Matalina[/eluser]
That's probably right. The code I posted above cronix's should fix that issue.
#9

[eluser]Perkin5[/eluser]
Sorry for delay in replying.

I think you were both right. Matalina's last code has fixed the problem and it now works correctly.

I'm very grateful - thanks very much to both of you and I shall make a careful note of the way it is expressed.




Theme © iAndrew 2016 - Forum software by © MyBB