adi_contact – Form field validation & spam prevention

This plugin is used in conjunction with zem_contact_reborn to provide the following functionality:

  • submitted field combination check
  • submitted field content validation
  • anti-spam measures

Once installed, a collection of new tags are available:

Use the above tags within a <txp:zem_contact> ... </txp:zem_contact> container. A full example of usage is provided at the end.


Form field combinations (adi_contact_combo)

Sometimes certain combinations of form fields are required from users, for example:

  • fill in at least one of “Likes”, “Dislikes”, “Other comments”
  • if “Phone number” is filled in then “Best time to call” is also required

For example to make sure the user fills in at least one field in a specified list:

<txp:zem_contact>
  ...
  <txp:zem_contact_textarea name="likes" label="Likes" required="0" />
  <txp:zem_contact_textarea name="dislikes" label="Dislikes" required="0" />
  <txp:zem_contact_textarea name="comments" label="Other comments" required="0" />
  <txp:adi_contact_combo names="likes,dislikes,comments"
    restrict="min" number="1" />
</txp:zem_contact>

Or if an extra field has to be filled in:

<txp:zem_contact>
  ...
  <txp:zem_contact_text name="phone" label="Your phone number"
    required="0" />
  <txp:zem_contact_text name="when" label="When should we call you?"
    required="0" />
  <txp:adi_contact_combo names="phone" require="when" />
</txp:zem_contact>

You can also generate custom error messages by using required="0" in the standard ZCR tags and required="1" in adi_contact_combo. For example:

<txp:zem_contact>
  ...
  <txp:zem_contact_text name="name" label="Name" required="0" />
  <txp:adi_contact_combo names="name" required="1" message="Don't be shy!"/>
</txp:zem_contact>

No real magic here – required="1" is actually the equivalent of restrict="equal" number="1".

adi_contact_combo – attributes

names="name list"

- comma separated list of field names that are of interest. Default = “” (none).

restrict="restriction"

- defines a minimum (min), a maximum (max), or an exact number (equal),

number="integer"

- number of fields, used in conjunction with restrict.

require="name list"

- comma separated list of field names that are required, depending on the presence of fields in names list.

required="boolean

- the specified field is required (useful for creating custom error messages). Default = “0” (not required).

checkbox="boolean

- the specified fields are checkboxes, so test for Yes/No. Default = “0” (not checkboxes).

message="text"

- alternative error message (see below for list of placeholders).


Form field validation (adi_contact_validate)

This enables the contents of fields to be validated, for example to check that a phone number field has been filled in properly:

<txp:adi_contact_validate names="home_phone" type="phone" />

adi_contact_validate – attributes

names="name list"

- comma separated list of field names that should be validated. Default = “” (none).

type="validation"

- the type of validation that is required:

  • alpha – alphabetic characters
  • alphanum – alpha-numeric characters
  • integer – digits only
  • numeric – a number (e.g. +0123.45e6 is a number)
  • phone – phone number (i.e. 0-9 + – . , () xX space)
  • url – a URL (with or without “http://”)
  • custom – use in conjunction with pattern

allow_whitespace="boolean"

- allow whitespace (i.e. space, tab, newlines etc) in addition to that specified in type. Default = “0” (No).

pattern="pattern string"

- a pattern that will be dropped straight into preg_match. Used in conjunction with type="custom". Default = “” (none).

message="text"

- alternative error message.


Spam prevention (adi_contact_spam)

This will prevent email header injection and stop emails that contain links (which is, after all, what spammers want to send you).

By default adi_contact_spam will check all fields, so if you want users to send you links then use the exclude attribute:

<txp:zem_contact>
  ...
  <txp:zem_contact_text name="website" label="Your website URL" />
  <txp:adi_contact_spam exclude="website" />
</txp:zem_contact>

adi_contact_spam – attributes

names="name list"

- comma separated list of field names that should be checked. Default = “” (all submitted fields).

exclude="name list"

- comma separated list of field names that should NOT be checked. Default = “” (none).

detect_mail_headers="boolean"

- detect the presence of to:, from:, bcc:, multipart, cc:, Content-Transfer-Encoding:, Content-Type:, Mime-Version: and flag as spam if found. Default = “1” (Yes).

detect_link_tags="boolean"

- detect the presence of [url, http, www, href and flag as spam if found. Default = “1” (Yes).

allow_www="boolean"

- allow the presence of www and do not flag as spam. Default = “0” (No).

message="text"

- alternative error message.


Error messages, labels and styling (adi_contact_labels & the ‘message’ attribute)

The ‘message’ attribute

The adi_contact tags will generate standard messages in English. The messages can be customised by using the message="" attribute:

  • alter the standard message
  • translate the standard message into other languages
  • use [NAMES] (‘names’ attribute), [REQUIRE] (‘require’ attribute) & [NUMBER] (‘number’ attribute) placeholders

The adi_contact error messages are placed in the same list as the standard zem_contact error messages.

adi_contact_labels – attributes

name1="label string 1" name2="label string 2"

- the labels to use in adi_contact error messages.

Important: The adi_contact_labels tag MUST be placed in the page/form before other adi_contact tags – otherwise your label strings won’t be picked up.

Example:

<txp:adi_contact_labels phone="Phone number" when="When to call"
  likes="Likes" dislikes="Dislikes" comments="Comments" />

Notes:

  • a label specified with adi_contact_labels does not have to match the actual label used in the form
  • labels specified with adi_contact_labels are only used in adi_contact error messages
  • the error messages will use labels in the following order of preference:
    • label specified in adi_contact_labels
    • form label retrieved from zem_contact (but only if a field has been filled in & submitted)
    • the field name (with first letter in upper case)

Styling

The standard zem_contact class of zemRequirederrorElement is automatically added to label and input elements when errors are found.

An extra class of adi_contact_error is also added to facilitate additional styling options.


The Full Monty Example

The following is an example of a user feedback form:

<txp:zem_contact to='me@example.com' subject='Feedback form example'>

  <p>Please fill in the form:</p>

  <-- the feedback form presented to the user -->
  <txp:zem_contact_text name="name" label="Name" />
  <txp:zem_contact_email name="email" />
  <txp:zem_contact_text name="phone" label="Your phone number" required="0" />
  <txp:zem_contact_text name="when" label="When should we call you?" required="0" />
  <txp:zem_contact_textarea name="likes" label="Likes" required="0" />
  <txp:zem_contact_textarea name="dislikes" label="Dislikes" required="0" />
  <txp:zem_contact_textarea name="comments" label="Other comments" required="0" />
  <txp:zem_contact_submit label="Send feedback" />

  <-- specify labels to be used in error messages -->
  <txp:adi_contact_labels phone="Phone number" when="When to call" comments="Comments" />

  <-- at least one of "likes,dislikes,comments" required -->
  <txp:adi_contact_combo names="likes,dislikes,comments" restrict="min" number="1" />

  <-- if "phone" supplied then "when" required as well; include custom error message -->
  <txp:adi_contact_combo names="phone" require="when"
    message="You've only given me [NAMES] but I need to know [REQUIRE] too" />

  <-- validate the phone number -->
  <txp:adi_contact_validate names="phone" type="phone" />

  <-- check for spam but allow user to send simple "www." URLs -->
  <txp:adi_contact_spam allow_www="1" />

</txp:zem_contact>

Notes:

  • the <txp:adi_contact_labels /> tag is placed before the other adi_contact tags
  • only the labels for phone, when & comments are specified because they are different from the actual form labels

Additional information

Support and further information can be obtained from the Textpattern support forum. A copy of this help is also available online. More adi_plugins can be found here.

Version 0.2 - Download Support