phpLens
 home  products examples manual  faq support forum  contact news  login store

Validation

Table of Contents

Validation

When you save a record, phpLens will automatically validate all fields based on the following criteiria. If the field has been defined as must fill field (using the mustFill property), the user is warned. If you require more detailed validation, we provide two ways of validating your data: Visual Basic style input mask, or Javascript validation.

Visual Basic Style Validation

X 0-9 and A-Z, must exist
x 0-9 and A-Z. This character is optional.
# 0-9, must exist.
90-9. 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


Javascript 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. For PHP programmers, Javascript regular expression syntax follows preg_match. Some examples follow:

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.

The second optional parameter is the error message on error.

Predefined Javascript Functions

The first character must be the equals (=) character.

Description Code
To validate email of the form: user@domain.com =EMAIL
Credit cards (not available 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 ugly but powerful: ^(\\\\S+@).+((\\\\.com)|(\\\\.edu)|(\\\\.gov)|(\\\\.int)|(\\\\.mil)|(\\\\.net)|(\\\\.org)|(\\\\.info)|(\\\\.biz)|(\\\\.name)|(\\\\.pro)|(\\\\.museum)|(\\\\.coop)|(\\\\.aero)|(\\\\.[a-z]{2,2}))$

This regular expression is defined as a constant in the phplens.inc.php file, and can be modified to suit your needs.

Javascript 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.

 

This documentation system is maintained using phpLens

email: info#phplens.com (change # to @)     telephone (malaysia): 60-3 7806 1216     fax (malaysia): 60-3 7806 1210