Client-side JavaScript validation.
Client-side JavaScript validationDemonstrates client-side validation. Fields are validated when they lose focus. The regular expression has to match the entire string (as in HTML5 patterns). Contrary to PHP preg_match() "/[a-z]/" does not match "aaa".
<?php
require_once '../../HtmlForm.php';
$form->addText('username', [
'label' => 'User name',
'required' => true,
]);
$form->addEmail('email', [
'label' => 'Email address',
]);
$form->addBoolean('accept', [
'label' => 'Accept terms and conditions.',
'required' => true,
]);
$form->addText('singleLetter', [
'label' => 'Single letter',
'required' => true,
'validator' => '/[a-zA-Z]/',
]);
$form->addText('letters', [
'label' => 'One ore more letters',
'required' => true,
'validator' => '/[a-zA-Z]+/',
]);
$form->process();
if ($form->validate()) {
echo('<a href="">back</a>');
echo('<pre>');
var_dump($form->getValues());
echo('</pre>');
$form->clearSession();
} else {
?>
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="../../lib/css/depage-forms.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="../../lib/js/validator.js"></script>
<script src="../../lib/js/effect.min.js"></script>
</head>
<body>
<?php
echo($form);
?>
</body>
<?php
}