Regular Expressions in cformsII
dVector.com provides the plugin cformsII for forms. This is a powerful and flexible plugin. In advanced verification of visitor submitted data, regular expressions are often used. See cformsII help especially the ‘Using regular expressions with form fields’.
In the dVector installation, there seems to be a small implementation issue. cformsII strips out single ‘\’s in the regular expression. To fix this problem simply place two ‘\’s instead of one in your regular expression. Unfortunately, every time you update settings, the ‘\’s are stripped. This means that if you use regular expressions in your form fields, you must double the slashes just before your last Update Settings.
Here are some regular expressions I use:
Telephone||^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$
Zip Code||^[0-9]{5}([- /]?[0-9]{4})?$
Website||^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$
Name||^[a-zA-Z][a-zA-Z',\.\- ]*[a-zA-Z\.]$
The same regular expressions only with doubled slashes:
Telephone||^\\([0-9]{3}\\)\\s?[0-9]{3}(-|\\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$
Zip Code||^[0-9]{5}([- /]?[0-9]{4})?$
Website||^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$
Name||^[a-zA-Z][a-zA-Z',\\.\\- ]*[a-zA-Z\\.]$
This is a bit of an inconvenience but once you have set your forms, there is usually very little reason to change them.
While the use of regular expressions does not eliminate invalid data, it certainly does cut down on erroneous user input. The Regular Expression Library provides an excellent resource for those simply looking for regular expression recipes.






