For a complete introduction to Interchange form field checks, please see the order check glossary entry.
Table of Contents
always_fail — always fail
Interchange 5.9.0:
Source: code/OrderCheck/always_fail.oc
Lines: 17
# Copyright 2006-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: always_fail.oc,v 1.2 2007-03-30 23:40:48 pajamian Exp $
CodeDef always_fail OrderCheck 1
CodeDef always_fail Description Always fails
CodeDef always_fail Routine <<EOR
sub {
my ($ref, $name, $value, $msg) = @_;
return (0, $name, $msg || errmsg('failed'));
}
EOR
always_pass — always pass
Interchange 5.9.0:
Source: code/OrderCheck/always_pass.oc
Lines: 16
# Copyright 2006-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: always_pass.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef always_pass OrderCheck 1
CodeDef always_pass Description Always succeeds
CodeDef always_pass Routine <<EOR
sub {
return (1, $_[1], '');
}
EOR
email_only — e-mail address is syntactically correct
Verification of the form field value succeeds if it contains a syntactically valid e-mail address.
Interchange 5.9.0:
Source: code/OrderCheck/email_only.oc
Lines: 24
# Copyright 2005-2009 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: email_only.oc,v 1.5 2009-01-28 07:38:26 racke Exp $
CodeDef email_only OrderCheck 1
CodeDef email_only Description Email address
CodeDef email_only Routine <<EOR
sub {
my($ref, $var, $val, $msg) = @_;
if($val and $val =~ /^[\040-\053\055-\077\101-\176]+\@[-A-Za-z0-9.]+\.[A-Za-z]+$/) {
return (1, $var, '');
}
else {
return (undef, $var,
$msg || errmsg("'%s' not an email address", $val )
);
}
}
EOR
exists — record exists in a database table
Verification of the form field value succeeds if it contains code that exists in the specified database table.
Foreign lookups are possible; see the section called “EXAMPLES”.
Example: Check for value in database table "products", with custom error message
FORM_FIELD_NAME=exists products Product not found in database
Example: Check for value in database table "products", in foreign column "price"
FORM_FIELD_NAME=exists products:price
Interchange 5.9.0:
Source: code/OrderCheck/exists.oc
Lines: 54
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: exists.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef exists OrderCheck 1
CodeDef exists Description Existing record
CodeDef exists Routine <<EOR
sub {
my($ref, $name, $value, $code) = @_;
$code =~ s/(\w+)(:+(\w+))?\s*//;
my $tab = $1
or return (0, $name, errmsg("no table specified"));
my $col = $3;
my $msg = $code;
my $db = database_exists_ref($tab)
or do {
$msg = errmsg(
"Table %s doesn't exist",
$tab,
);
return(0, $name, $msg);
};
my $used;
if(! $col) {
$used = $db->record_exists($value);
}
else {
#::logDebug("Doing foreign key check, tab=$tab col=$col value=$value");
$used = $db->foreign($value, $col);
}
#::logDebug("Checking exists, tab=$tab col=$col, used=$used");
if($used) {
return (1, $name, '');
}
else {
$msg = errmsg(
"Key %s does not exist in %s, try again.",
$value,
$tab,
) unless $msg;
return(0, $name, $msg);
}
}
EOR
filter — value passes through specified filter unmodified
Verification of the form field value succeeds if it passes the specified filter unaltered.
For a list of all possible filters, see Interchange Reference Pages: Filters. Filters that unconditionally modify input data are generally not suitable for use with this order check; the data is always changed so it always fails the validation.
Interchange 5.9.0:
Source: code/OrderCheck/filter.oc
Lines: 35
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: filter.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef filter OrderCheck 1
CodeDef filter Description Passes filter unchanged
CodeDef filter Routine <<EOR
sub {
my ($ref, $name, $value, $code) = @_;
my $message;
my $filter;
$code =~ s/\\/\\\\/g;
if($code =~ /(["']).+?\1$/) {
my @code = Text::ParseWords::shellwords($code);
$message = pop(@code);
$filter = join " ", @code;
}
else {
($filter, $message) = split /\s+/, $code, 2;
}
my $test = Vend::Interpolate::filter_value($filter, $value, $name);
if($test ne $value) {
$message ||= errmsg("%s caught by filter %s", $name, $filter);
return ( 0, $name, $message);
}
return (1, $name, '');
}
EOR
future — date is a future date
Verification of the form field value succeeds if the passed time and date are in the future, compared to the current time.
It is possible to specify a minimum accepted time difference between the current and passed date. The specification can be any valid interval.
Example: Check for future date, displaying a custom error message
FORM_FIELD_NAME=future "Date must be in the future"
Example: Check for future date, at least 2 days ahead
FORM_FIELD_NAME=future 2 days "Date must be at least two days ahead"
Example: Check for date within 60 minutes behind the current time
FORM_FIELD_NAME=future -60 minutes "Time must be within an hour behind"
This order check makes sense with date fields and widgets only.
The time difference can be specified as a negative value as well, effectively allowing you to check whether the passed date is "too behind" the current date.
Interchange 5.9.0:
Source: code/OrderCheck/future.oc
Lines: 50
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: future.oc,v 1.5 2007-03-30 23:40:48 pajamian Exp $
CodeDef future OrderCheck 1
CodeDef future Description Future date
CodeDef future Routine <<EOR
sub {
my($ref, $name, $value, $code) = @_;
my $message;
my @code = Text::ParseWords::shellwords($code);
if($code =~ /(["']).+?\1$/) {
$message = pop(@code);
}
my $adjust = join " ", @code;
if(! $message) {
$message = errmsg(
"Date must be in the future at least %s",
$adjust,
);
}
if($value =~ /\0/) {
$value = Vend::Interpolate::filter_value(
'date_change',
$value,
);
}
my $current = Vend::Interpolate::mvtime(
undef,
{ adjust => $adjust },
"%Y%m%d%H%M",
);
# reject invalid dates
if($value !~ /^[12]\d\d\d[01]\d[0123]\d(?:[0-2]\d[0-5]\d(?:[0-5]\d)?)?$/) {
return (0, $name, $message);
}
if($value lt $current) {
return (0, $name, $message);
}
return (1, $name, '');
}
EOR
isbn — string is a valid ISBN-10/ISBN-13 code
Verification of the form field value succeeds if the passed value is a valid ISBN-10/ISBN-13 code.
The check can be advised to only accept ISBN-10 resp. ISBN-13 code.
Interchange 5.9.0:
Source: code/OrderCheck/isbn.oc
Lines: 71
# Copyright 2008,2009 Interchange Development Group
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
CodeDef isbn OrderCheck 1
CodeDef isbn Description ISBN-10/ISBN-13 check digit verification
CodeDef isbn Routine <<EOR
sub {
my($ref, $var, $val, $msg) = @_;
my($len);
if ($msg =~ s/^\s*(10|13)\s*//) {
$len = $1;
}
$val =~ s/[^\dXx]//g; # weed out non-digits
if ($val) {
my @digits = split("", $val);
my $sum = 0;
my $check_digit = 0;
my $modulo;
if (@digits == 10 ) {
# ISBN-10 number
if ($len == 13) {
return (0, $var, errmsg("'%s' not a valid isbn-13 number", $val));
}
for(my $i=10; $i > 0; $i--) {
my $d = $digits[10 - $i];
if ($d =~ /[Xx]/) {
if ($i == 1) {
$d = 10;
}
else {
return (undef, $var, errmsg("'%s' not a valid isbn number", $val));
}
}
$sum += $d * $i;
}
return ( $sum%11 ? 0 : 1, $var, '' );
} elsif (@digits == 13) {
# ISBN-13/EAN number
if ($len == 10) {
return (0, $var, errmsg("'%s' not a valid isbn-10 number", $val));
}
for (my $i = 0; $i < 12; $i++) {
if ($i % 2) {
$sum += 3 * $digits[$i];
}
else {
$sum += $digits[$i];
}
}
if ($modulo = $sum % 10) {
$check_digit = 10 - $modulo;
}
if (pop(@digits) == $check_digit) {
# verification successful
return (1, $var, '');
}
}
}
return (undef, $var, errmsg("'%s' not a valid isbn number", $val));
}
EOR
length — string is within specified length limits
Example: Minimum length of 8 characters
password=length 8 Please enter at least 8 characters for your password.
Example: Length between 6 and 32 characters
username=length 6-32 Size limits exceeded (6-32 characters)
Interchange 5.9.0:
Source: code/OrderCheck/length.oc
Lines: 39
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: length.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef length OrderCheck 1
CodeDef length Description String length
CodeDef length Routine <<EOR
sub {
my($ref, $name, $value, $msg) = @_;
$msg =~ s/^(\d+)(?:\s*-(\d+))?\s*//
or return undef;
my $min = $1;
my $max = $2;
my $len = length($value);
if($len < $min) {
$msg = errmsg(
"%s length %s less than minimum length %s.",
$name,
$len,
$min) if ! $msg;
return(0, $name, $msg);
}
elsif($max and $len > $max) {
$msg = errmsg(
"%s length %s more than maximum length %s.",
$name,
$len,
$max) if ! $msg;
return(0, $name, $msg);
}
return (1, $name, '');
}
EOR
match — CGI value matches another CGI value
Verification of the form field value succeeds if the passed value matches that of another specified CGI variable.
Interchange 5.9.0:
Source: code/OrderCheck/match.oc
Lines: 24
# Copyright 2007 Interchange Development Group (http://www.icdevgroup.org/)
# Licensed under the GNU GPL v2. See file LICENSE for details.
# $Id: match.oc,v 1.1 2007-05-04 14:36:00 mheins Exp $
CodeDef match OrderCheck 1
CodeDef match Description Matches another CGI variable, possibly for password verify
CodeDef match Routine <<EOR
sub {
my($ref, $name, $value, $msg) = @_;
$msg =~ s/^\s*(\w[-\w]*)\s*//
or return undef;
my $other = $1;
if($ref->{$other} ne $value) {
$msg = errmsg(
"%s doesn't match %s.",
$name,
$other,
) if ! $msg;
return(0, $name, $msg);
}
return (1, $name, '');
}
EOR
natural — number is a natural number
Verification of the form field value succeeds if the passed value is a natural number, greater than zero.
Example: Natural number check, displaying a custom error message
FORM_FIELD_NAME=natural "Number is not a natural number"
Up to Interchange 5.5.2, there was a problem in implementation of this order check which allowed negative values and zero to pass as valid.
Interchange 5.9.0:
Source: code/OrderCheck/natural.oc
Lines: 30
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: natural.oc,v 1.4 2008-04-28 12:08:38 docelic Exp $
CodeDef natural OrderCheck
CodeDef natural Description Natural number
CodeDef natural Routine <<EOR
sub {
my ($ref, $name, $value, $code) = @_;
if ($value and $value > 0 and "$value" eq int($value)) {
return (1, $name, '');
}
$code =~ s/\\/\\\\/g;
$code =~ s/^\s*(["'])(.+?)\1$/$2/;
if ($code =~ /\S/) {
return (0, $name, $code);
} else {
return (0, $name, 'no natural number');
}
}
EOR
numeric
Interchange 5.9.0:
Source: code/OrderCheck/numeric.oc
Lines: 23
# Copyright 2010 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
CodeDef numeric OrderCheck
CodeDef numeric Description Numeric
CodeDef numeric Routine <<EOR
sub {
my ($ref, $name, $value, $msg) = @_;
use Scalar::Util qw/looks_like_number/;
if (looks_like_number($value)) {
return (1, $name, '');
}
else {
return (0, $name, defined($msg) ? $msg : 'not numeric');
}
}
EOR
numeric_strict
Interchange 5.9.0:
Source: code/OrderCheck/numeric_strict.oc
Lines: 21
# Copyright 2010 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
CodeDef numeric_strict OrderCheck
CodeDef numeric_strict Description Numeric (strict)
CodeDef numeric_strict Routine <<EOR
sub {
my ($ref, $name, $value, $msg) = @_;
if (defined($value) and $value =~ /\A-?\d+(?:\.\d+)?\z/) {
return (1, $name, '');
}
else {
return (0, $name, defined($msg) ? $msg : 'not strict numeric');
}
}
EOR
regex — value matches regular expression
Allows you to specify a regular expression to match against the supplied value. Useful for many cases where no existing order check is available.
Example: Fiscal data
fiscal_data=regex ^\d\d-[A-Z\d]{9}-\d{4}-[A-Z\d]{10}-\d{4}-\d{4}$ "Invalid format"
Interchange 5.9.0:
Source: code/OrderCheck/regex.oc
Lines: 45
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: regex.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef regex OrderCheck 1
CodeDef regex Description Regular expression match
CodeDef regex Routine <<EOR
sub {
my($ref, $name, $value, $code) = @_;
my $message;
$code =~ s/\\/\\\\/g;
my @code = Text::ParseWords::shellwords($code);
if($code =~ /(["']).+?\1$/) {
$message = pop(@code);
}
for(@code) {
my $negate;
s/^!\s*// and $negate = 1;
my $op = $negate ? "!~" : '=~';
my $regex = qr($_);
my $status;
if($negate) {
$status = ($value !~ $regex);
}
else {
$status = ($value =~ $regex);
}
if(! $status) {
$message = errmsg(
"failed pattern - %s",
"'$value' $op $_"
) if ! $message;
return ( 0, $name, $message);
}
}
return (1, $name, '');
}
EOR
relative_filename — value qualifies as relative filename
Interchange 5.9.0:
Source: code/OrderCheck/relative_filename.oc
Lines: 24
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: relative_filename.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef relative_filename OrderCheck 1
CodeDef relative_filename Description Relative filename
CodeDef relative_filename Routine <<EOR
sub {
my ($ref, $name, $value, $code) = @_;
if ($value =~ /\S/ && $value !~ /^\.{1,2}$/
&& ! Vend::File::absolute_or_relative($value)) {
return (1, $name, '');
} else {
$code ||= errmsg('filename not relative');
return (0, $name, $code);
}
}
EOR
unique — record doesn't exist in a database table
This profile checks whether a matching record exists in a database table. It succeeds if there is no such record.
Example: Ensure unique email address
email=email_only Please enter a valid email address. &and email=unique userdb:email An account with this email address already exists.
This check doesn't guarantee unique records, as there is a small window of time between performing this check and creating a new record.
Interchange 5.9.0:
Source: code/OrderCheck/unique.oc
Lines: 52
# Copyright 2005-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: unique.oc,v 1.3 2007-03-30 23:40:48 pajamian Exp $
CodeDef unique OrderCheck 1
CodeDef unique Description Unique record
CodeDef unique Routine <<EOR
sub {
my ($ref, $name, $value, $code) = @_;
$code =~ s/(\w+)(:+(\w+))?\s*//;
my $tab = $1
or return (0, $name, errmsg("no table specified"));
my $col = $3;
my $msg = $code;
my $db = database_exists_ref($tab)
or do {
$msg = errmsg(
"Table %s doesn't exist",
$tab,
);
return(0, $name, $msg);
};
my $used;
if(! $col) {
$used = $db->record_exists($value);
}
else {
#::logDebug("Doing foreign key check, tab=$tab col=$col value=$value");
$used = $db->foreign($value, $col);
}
#::logDebug("Checking unique, tab=$tab col=$col, used=$used");
if(! $used) {
return (1, $name, '');
}
else {
$msg = errmsg(
"Key %s already exists in %s, try again.",
$value,
$tab,
) unless $msg;
return(0, $name, $msg);
}
}
EOR