Having trouble with commas in regex conditions. May just be my naivety with regex, but this works...
regexp[/^[0-9a-zA-Z\s-_.'()\/:#&]+$/]
...and this doesn't...
regexp[/^[0-9a-zA-Z\s-_.,'()\/:#&]+$/]

Note the only difference is the comma. I've tried escaping that character as well.

CommentFileSizeAuthor
#3 commas_in_regex-1425268-3.patch556 bytesdoublejosh

Comments

doublejosh’s picture

I've also tried using \pL to get the comma character included. Still no luck.
Doesn't work: regexp[/^[0-9a-zA-Z-_.'()\/:#&\pL\"\\\ \\,]*$/]
(note double backslash because rule is in quotes.)

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id){
    case 'user_register' :
      if(module_exists('fapi_validation')) {
        $fields = array('first_name' => 40, 'last_name' => 80, 'title' => 80, 'organization' => 80);
        foreach ($fields as $fKey => $fLen) {
          $form['Profile']['profile_'.$fKey]['#rules'] = array(
            array('rule' => 'length[1, '.$fLen.']', 'error' => '%field is an invalid size (max length '.$fLen.').'),
            array('rule' => "regexp[/^[0-9a-zA-Z-_.'()\/:#&\pL\"\\\ ]*$/]",
              'error' => '%field can only include letters, numbers, and basic characters.'),
          );
          $form['About You']['profile_'.$fKey]['#filters'] = array('trim');
        }
      }
      break;
  } // end switch
}
doublejosh’s picture

Priority: Normal » Major

After using this tool to test my rule against some text, I'm convinced there's a problem with this module. Seems that when there's a comma in the regex rule it ends the rule string and breaks it. The rule gets broken apart in $params. Discovered via...

function fapi_validation_rule_regexp($value, $params) {
  dsm($params[0]);
  return (bool) preg_match($params[0], (string) $value);
}

Looks like that happens here:

$params[] = preg_split('/ *, */', $rs[3]);

...which I believe is for use with length.

doublejosh’s picture

StatusFileSize
new556 bytes

Ok, here's the patch.

doublejosh’s picture

Status: Active » Needs review
doublejosh’s picture

Issue tags: +formapi, +regex, +field validation
doublejosh’s picture

Title: Commas in regex a problem » Commas in regex rules breaks validation
doublejosh’s picture

This seems like a big deal.
Made the module not usable without the patch for fairly normal text restriction to include commas.

pedrofaria’s picture

Fix committed.

Thanks and sorry about the ruge delay.

pedrofaria’s picture

Assigned: Unassigned » pedrofaria
doublejosh’s picture

Awesome. Thanks. Excited to see it get in.

Switching a site to D7 soon, in case I end up needing to make the patch for that too.

pedrofaria’s picture

This is already ported to D7 :)

just enjoy!

thanks!

pedrofaria’s picture

Status: Needs review » Fixed
doublejosh’s picture

Status: Fixed » Closed (fixed)
doublejosh’s picture

Issue summary: View changes

bold