Back
validation
Editing, Updating and Creating Records
validation
Javascript or input mask for data and error message on error
Default:
We provide 5 types of validations.

1. Input mask. For example, #### means enter exactly 4 digits.
2. Special validation functions. For example, =EMAIL means that this field should be checked for the standard internet email format.
3. Regular expression. Regular expression is activated if first character is =.
4. Javascript expression. Javascript is activated if the first character is %. This is since phpLens 3.
5. Javascript custom function validation. Since phpLens 4.2.

When we use input mask validation, we can enter in the input mask the following:
X Character must be 0-9 or A-Z and is compulsory
x 0-9 or A-Z. This character is optional.
Z Character must be 0-9 or A-Z or _ and is compulsory.
z 0-9 or A-Z or _. This character is optional.
# Character must be 0-9 or ./+/- and is compulsory.
90-9 or ./+/-. This character is optional.
+Positive numbers only
&Any character, must exist
?Any character, optional
escape character
>

Uppercase

<Lowercase


Examples
Telephone numbers with no area code.
7 digits must be filled, but some newer phone numbers have 8 digits, so make 1 character optional.
9###-####
Car part number with the format: 3 digit number, a hyphen followed by a 3 or 4 letter code ###-XXXx



Special Validation Functions
The first character must be the equals (=) character. The code is not case-sensitive.

Description Code
To validate email of the form: user@domain.com =EMAIL
Credit cards (not yet) =CREDITCARD


To validate email a regular expression is used. This regular expression
is defined as a constant called PHPLENS_EMAIL_REGEX. You can override it by defining this constant before phplens.inc.php is included in your main php file.

The regular expression used is the ugly but powerful: ^(S+@).+((.com)|(.edu)|(.gov)|(.int)|(.mil)|(.net)|(.org)|(.info)|(.biz)|(.name)|(.pro)|(.museum)|(.coop)|(.aero)|(.[a-z]{2,2}))$


Regular Expressions
The first character must be the equals (=) character. All subsequent characters are treated as standard Javascript regular expression characters. /expr/ delimiters are not required.

Value must be either CAR or BOOK, case-insensitive. The $ at the end indicates that no further characters will be accepted. ([Cc][Aa][Rr])|([Bb][Oo][Oo][Kk])$
Value must begin with 'A' or 'a' followed by any other characters [aA].*


Remember that the regular expressions are case-sensitive. You do not need to enter the ^ and $ in ^expr$ at the beginning and end of the regular expression as it is done automatically for you by phpLens.

The second optional parameter is the error message on error.


Javascript Expression

The first character must be % to activate this. Any hash (#) character in the expression is substituted with the field value.

To check if a value entered is between 10 to 20, use:

$lens->validation='Col^%10 <= # && # <= 20';


Encoding

Because ^ is used by regular expressions and phpLens as a seperator, we also allow a raw url encoded format. If the first and last characters of a validation setting are both '/', then the contents are raw url encoded as in the example below:

# =^[0-9][0-9].[0-9]$
$lens->validation='Col^/%3D%5E%5B0-9%5D%5B0-9%5D%5C.%5B0-9%5D%24/';


Javascript Custom Validation Functions


You can also call javascript functions in phpLens 4.2 or later to validate your code. Assume you want to validate the field CM_ID based on the following rules:

  1. If it is not filled, than allow the form submit to succeed.
  2. If CM_ID is zero or negative than let the default error message appear.
  3. If CM_ID is greater than zero than check that the field CM_NAME is filled.

In PHPLens, prefix javascript with %, and place # where you want the field value to be:
$lens->validation = 'CM_ID^%validatefn(#)';

Assume that the applet id is CMAPPLET, so the form name will be phplens_CMAPPLET_edit. To find out the field names, you will have to check the HTML output generated by phpLens. The general rule is the field name is prefixed by lens_F{$type}_, where {type} is the internal phpLens/ADOdb data type. Here is the sample javascript code:
function validatefn(val)
{
if (val.length == 0) return true; // criteria (1)
if (val <= 0) return false; // criteria (2)
var myform = document.phplens_CMAPPLET_edit
if (!myform.lens_FC_CM_NAME.value) { // criteria (3)
alert('The name field must be filled');
return '';
}
return true;
}

The javascript function should return:
  • true if validation succeeded
  • false if validation fails and you want the default error message to appear
  • '' (empty string) if you want to fail the validation and display your own alert.


Note also that this javascript function will only be called if the field is non-empty, or is must fill. If the field is empty and not compulsory, then the javascript will not be called.

Syntax
$lens = "ColumnName^Validation Mask^Invalid Error Message";

$lens->validation =
  "PHONE^#######9^Phone number must be 7 or 8 digits";

To treat a column as an email column:
$lens->validation = 'mailaddress^=EMAIL';

# value must be greater than 0
$lens->validation='Col^%0 < #';

 Basic:Yes  Advanced/Enterprise:Yes  DynamicEdit:Yes   [Version 1.0]