How to Validate Forms in Joomla
Forms in Joomla can be validated in two ways: (i) Client-side and (ii) Server-side.
Client-side validation is done via javascript while the user is filling in the form fields. Server-side validation is done after having submitted the form and will normally return to the form when not validated with some extra messages.
Client Side Form Validation
Step 1
First, you need to add a behavior to your form view template. For Joomla versions from 3.4:
JHtml::_('behavior.formvalidator');
Step 2
Then, add the form-validate class to your form.
<form class="form-validate">
Step 3
Then, you need to add a validation class to your form field declarations. The following classes are available:
- required
- validate-username
- validate-password
- validate-numeric
- validate-email
Step 4
Finally, add the validate class to your submit button. This will cause an onclick handler to be added that validates the whole form. The form will only be submitted if it is valid.
<input type="submit" name="submit" class="validate btn btn-primary"/>