4.4. JavaScript validation

phpMyEdit users can benefit from the JavaScript scripting language, when validating input values before the form is submited.

Required fields

Simple validation JavaScript can be generated to prevent null entries by the user. If an entry is required for a particular field, set the ['js']['required'] option to true.

Example 4-12. Required fields

$opts['fdd']['col_name']['js']['required'] = true; 
$opts['fdd']['col_name']['js']['required'] = false;

Regular expressions

JavaScript regular expressions can be a powerful way to make interactive input validation. When used, input must match the desired regular expression defined in the  $opts['fdd']['col_name']['js']['regexp']. If a field does not match, a JavaScript alert will be invoked to force the user to change entered value.

Example 4-13. Regular expression example

$opts['fdd']['col_name']['js']['regexp'] = '/^[0-9]*$/';

Regular expressions are written in Perl compatible style. The expression above allows the form to submit only when numeric characters 0-9 are entered in the field. An empty value is also allowed, but you can use ['js']['regexp'] in combination with ['js']['required'] to prevent empty entries.

Hints

When the ['js']['required'] option is used and the value entered is empty, or the characters entered do not match the defined ['js']['regexp'], a warning with the default message is displayed. This message can be changed using ['js']['hint']. This is especially useful for advising the user about field-specific data input restrictions.

Example 4-14. JavaScript hint

$opts['fdd']['col_name']['js']['hint']   = 'Please enter only numbers in the "col_name" field.';


Bootstrap phpMyEdit PHP Form Generator