Perl Form Builder ::Features ::Download ::Tutorial ::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute
Note: The mailing list is has been replaced by the Google FormBuilder Group


1. Intro to FormBuilder
2. A Simple Form
3. Form Method
4. Input Validation
5. Field Options
6. Multi-Select Lists
7. Field Data
8. Field Attributes
9. HTML Formatting
10. Custom Headers
S. Summary


1. Trickier Forms
2. Perl References
3. Default Values
4. CGI Stickiness
5. JavaScript Actions
6. Input Validation 2
7. Multiple Submits
8. JavaScript Actions 2
9. Modules & mod_perl
10. Smartness
S. Summary


1. Remaining Features
2. Intro to Templates
3. HTML::Template
4. HTML::Template 2
5. Template Toolkit
6. Exploiting render()
7. Multi-Screen Apps
8. Multi-Screen Apps 2
9. Multi-Screen Apps 3
10. Custom Messages
S. Summary

Intermediate Tutorial - Step 6: Input Validation 2 Print

<< JavaScript Actions | Multiple Submits >>

The simple validation with built-in patterns is often all that most people need. However, occassionally you may run into situations where you need to specify an exact regular expression, or set of values which the data must be within.

Doing so is easy. For each field you list in the validate option, you can specify the following:

  • The name of a built-in pattern, as we saw previously
  • A custom regular expression, in single quotes
  • An arrayref of values, of which the field must be one
  • A hashref of JavaScript and Perl-specific validations
So, tossing all these together, one could write a validation set like the following:
$form = CGI::FormBuilder->new(
             fields   => [qw/full_name username email dept_num
                             password confirm_password/],
             validate => {
                full_name => '/\w+\s+\w+.*/',
                username  => '/^[a-zA-Z]\w{5,7}$/',
                email     => 'EMAIL',
                dept_num  => [110923, 398122, 918923, 523211],
                password  => '/^[\w.!?@#$%&*]{6,8}$/'
                confirm_password => {
                      javascript => '== form.password.value',
                      perl       => 'eq $form->field("password")'
This would create both JavaScript and Perl conditionals on the fly that would ensure:
  • name is at least 2 words long
  • username starts with a letter, and is 6-8 characters total
  • email is a valid email address
  • password is 6-8 of the specified characters
  • confirm_password is the same as the password field
Remember, any regular expressions you specify must be enclosed in single quotes because they need to be used for both JavaScript and Perl code.

To get a sense of how much is done for you, take a look at this script in action, as well as the HTML source.

<< JavaScript Actions | Multiple Submits >>

FormBuilder is © Nate Wiger, with contributions from many people.