I suggest a pattern-based validation method for novice users who find regular expressions difficult. Something like:

A = [a-z]|[A-Z]
9 = [0-9]
- special regex characters escaped

This would allow novice user to easily construct validation rules for:
North American phone numbers: (999) 999-9999
Canadian postal codes: A9A 9A9

Such a pattern-based system could use the existing regex code.

Comments

svendecabooter’s picture

Status: Active » Postponed

Good idea :)
I'm not regex wizard though, so some thought will have to go into something like this.
I'm postponing this for now unfortunately, as I don't see myself implementing this any time soon realistically...

DrewMathers’s picture

svendecabooter,

I too look at regular expressions and see punctuation barf. A Google search on this problem has not revealed an answer. However, the people who dream in regular expressions have assembled a library of patterns you could use to create dozens of specialized validations (e.g. Danish phone number, Austrian postal code, etc.)

DrewMathers’s picture

Status: Postponed » Needs review
StatusFileSize
new2.1 KB

Here is a patch that adds this feature.

I chose to use the replacement characters @ and # because this is what the Geonames module gets from the Geonames.org database of international postal code patterns. I had ideas of integrating this with Webform Validation, but it requires a Postal Code field to be dependent on Country field, similar to #917020: Make component(s) required depending on the value of another field

DrewMathers’s picture

StatusFileSize
new2.07 KB

Replaced my ugly PHP version detection hack.

DrewMathers’s picture

Title: Pattern-based "regex lite" validation » Pattern sub-module
Version: 6.x-1.3 » 6.x-1.x-dev
StatusFileSize
new19.39 KB

I have re-done this patch as sub-module rather than patching Webform Validation code.

DrewMathers’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
StatusFileSize
new19.46 KB

Here is a version of the Pattern sub-module for Drupal 7

liam morland’s picture

Please don't include license files; they are added automatically by the drupal.org packaging script.

If this becomes part of Webform Validation, it would be part of core, not a separate module, so the patch should be made up that way.

I am hesitant to make up a new pattern matching language, even if it is simple. I note that the Geonames database uses | like rexeg does, but your patch treats | as a regular character. Is there a preexisting simple pattern language we could use as a reference to avoid creating yet another standard?

liam morland’s picture

Status: Needs review » Needs work
DrewMathers’s picture

Liam,

There is a D6 version (comment #4) that implements this in Webform Validation core. However, I got the impression that svendecabooter intended new validators to be separate modules.

Usually applications just make up their own wildcard languages. The only one I know that is an official standard is Wildmat which is now part of NNTP. Good catch on the Geonames use of the OR operator. I missed that.

Thoughts?

liam morland’s picture

I would accept this as an additional validator. Please make a patch implementing this along side the other validators in the core module. If it is not too difficult, allowing the OR operator would create alignment with Geonames and would increase the versatility of this validator. Please use _webform_validation_preg_quote() which takes care of the PHP 5.3 version detection.

DrewMathers’s picture

Title: Pattern sub-module » Pattern validator
Status: Needs work » Needs review
StatusFileSize
new2.39 KB

Here is the patch as reqested above.

liam morland’s picture

Thanks for the patch. There are some coding standards issues with it. Please review the module with Coder Review. Remove the @diag.

Can you come up with an example other than postal codes? I plan to write an integration with the Postal Code validation module, which does a more precise job than can be done with simple pattern matching.

DrewMathers’s picture

StatusFileSize
new2.33 KB

This version of the patch passes Coder Code Review. I wanted to keep at least the UK Postal Code example in the help text because it is a real world example of the use of the OR operator.

liam morland’s picture

What are the brackets for in this regex and why it is so complicated? I don't think we need to do whitespace removal. Wouldn't it work to just replace "\|" with "|"?

$pattern = preg_replace('/(\\s*)(\\\\)(\\|)(\\s*)/', ')|(', $pattern); // Un-escape "|" operator

Files should always end with a end-of-line character to avoid "\ No newline at end of file" messages.

DrewMathers’s picture

The whitespace removal around the "|" is essential. All validation tests fail without it. Consider "pattern1 \| pattern2". Directly substituting "\|" wtih "|" leaves a space after pattern1 and a space before pattern2. Regex treats these spaces being included in the pattern. The user's form entry will never include them, so validation always fails.

The brackets are my abundance of caution owing to the low order of precedence of the OR operator. It might be possible to omit them, but I didn't feel like risking it.

I guess there is no End of Line character at the end of webform_validation_validators.inc. I'll put in a feature request ;)

liam morland’s picture

What if the space is part of the pattern? People should separate pattern options with a pipe by itself, not surrounded by spaces.

Your patch removes the EOL character from webform_validation_validators.inc.

DrewMathers’s picture

It is not possible to submit form data with a leading or trailing space. Webform trims them, even for textarea fields.

Is the EOL a blocker issue to committing this patch?

liam morland’s picture

Status: Needs review » Reviewed & tested by the community

Right about space trimming. Thanks.

The EOL is not blocking it. Your patch also added trailing whitespace. In both cases, my editor removes them automatically.

liam morland’s picture

Status: Reviewed & tested by the community » Fixed
liam morland’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Fixed » Patch (to be ported)
DrewMathers’s picture

Right about space trimming. Thanks.

I know this is a day late and a dollar short but further testing on whitespace trimming showed something interesting. Even though form data ends up trimmed in Webform's output, it arrives into Webform Validator un-trimmed. However, I don't think this has any implications for the Pattern validator.

DrewMathers’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new2.04 KB

D6 backport patch

This backport has a dependency on
#1122982-6: Words blacklist regex restriction

liam morland’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Issue summary: View changes
Status: Needs review » Closed (fixed)

Drupal 6 is no longer supported.