EllisLab text mark
Advanced Search
     
Form validation regex_match
Posted: 29 June 2012 01:08 PM
Joined: 2011-05-17
156 posts

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:

$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?

 
Posted: 29 June 2012 01:20 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-03-31
205 posts

The regex string is not properly formatted

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

  
return  TRUE;
 

so I think you need to do

$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.

$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.

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

That should match only alpha characters and space.

 Signature 

Tell me and I forget. Teach me and I remember. Involve me and I learn. ~ Benjamin Franklin

 
Posted: 29 June 2012 01:34 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-05-17
156 posts

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.

 
Posted: 29 June 2012 01:36 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-03-31
205 posts

Try copying and pasting again.  It’s possible you hit my mistake I fixed a few seconds later.

 Signature 

Tell me and I forget. Teach me and I remember. Involve me and I learn. ~ Benjamin Franklin

 
Posted: 29 June 2012 01:43 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-05-17
156 posts

No, still same problem and php error. I noticed the changes in your code but they didn’t seem to do the trick.

 
Posted: 29 June 2012 01:46 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-03-31
205 posts

try this /^[a-zA-Z\s]+$/

 Signature 

Tell me and I forget. Teach me and I remember. Involve me and I learn. ~ Benjamin Franklin

 
Posted: 29 June 2012 01:46 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4545 posts

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:

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.

 Signature 
 
Posted: 29 June 2012 01:48 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2010-03-31
205 posts

That’s probably right.  The code I posted above cronix’s should fix that issue.

 Signature 

Tell me and I forget. Teach me and I remember. Involve me and I learn. ~ Benjamin Franklin

 
Posted: 29 June 2012 06:55 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-05-17
156 posts

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.