Regular expressions often require escape characters. If the regular expression below is entered, the escape characters are removed. I tried double escaping (\\/\\.\\+\\(\\)), but each time you save the snippet you have to remember to double the escapes again.

if (!preg_match('/^[-0-9 \/\.\+\(\)]+$/', $phone)) {
  form_set_error('submitted][phone', t('Phone number contains invalid characters.'));
}

Comments

quicksketch’s picture

Category: bug » support
Status: Active » Closed (fixed)

This is a PHP coding question. I don't provide support on writing your own custom validation or submission handlers.

FYI: Escaping characters is not needed when inside square brackets (other than "-"). Check up on your RegEx syntax.

toomanypets’s picture

Status: Closed (fixed) » Active

Thanks for the fast response! I'm pretty sure this is not a coding question, and I am keenly aware that you have no interest in debugging others' code -- no problem.

But the issue is ...

If I include a backslash anywhere in the "Additional validation" or "Additional processing" fields, the backslash is removed.

Try this.
1. Type \\\\\ the press Save configuration. Three of the backslashes have been removed.
2. Press Save configuration again. Another backslash has been removed.
3. Press Save configuration again. There's nothing left :(

This example may be silly, but it illustrates the underlying problem quite well. A real world example (where we can both agree that the RegEx syntax is correct) where this causes problems is:

    $pattern = '/\D/';
    $replacement = '';
    $phone = preg_replace($pattern, $replacement, $phone);
    if (strlen($phone) < 7) {
      form_set_error('submitted][phone', t('Phone number must contain at least 7 digits.'));
    }

The backslash in front of the D is removed when you save the form, trashing the pattern.

Thank you very much for your help.

toomanypets’s picture

Category: support » bug
toomanypets’s picture

Priority: Normal » Minor
Status: Active » Closed (cannot reproduce)

Since my original report I have updated both Webform (now running 6.x-3.2) and Webform PHP (now running 6.x-3.x-dev 2010-Aug-19). I can no longer reproduce this issue, but based on release notes I have no reason to believe that any codes changes were made that would have addressed this. Strange -- solar flares maybe...

Closing issue.