Validating your Joomla forms with ease & without bloating your apps

Written by Parth Lawate Friday, 16 April 2010 14:43
Rate this item
(11 votes) looking at some extensions, i realized that a whole lot of Joomla extensions use a lot of custom js to add validations to their forms. While this might be needed in some cases, 90% of the times you should be able to use the inbuilt Joomla validation available via the mootools validate.js . Working using in built Joomla functions will help keep you code free from excessive external js & reduce the bloat. A lot of times it will also help reduce JS conflicts.

Ironically i was prompted to write this blog post when we discovered one of our old extensions having this very issue ! While making the corrections i thought a small blog about using native Joomla validation might be helpful to a lot of devs.

So here is how you go about adding Joomla form validation...

Initiate the Joomla validate.js

In order to include validation, call the validate.js as shown below.


Identify the form that needs to be validated by giving it a class

The form that you need to validate needs to be given a class "form-validate"

<form id="myForm" class="form-validate" method="post"></form>

What can be validated ?

You can do all the standard validations needed.

  • required
  • validate-username
  • validate-password
  • validate-numeric
  • validate-email

You can also create custom validation handlers by using "validate-[custom]".

Here's an example :

<input type="text" name="qty" id="qty" size="30"  class="required validate-numeric"/>

This will validate the field for numeric values & also set it to required.

Setting Custom Handlers

In order to validate field types not supported like for date you can write your own handler for a class & call it after validate.js

Window.onDomReady(function() {
document.formvalidator.setHandler('date', function(value) {
return regex.test(value);

This can take care of the Javascript side validation. You can additionally add checks in the file that you are posting data to to make sure the form is validated.

defined( '_JEXEC' ) or die( 'Restricted access' );  //Verify Joomla enabled
$jAp=& JFactory::getApplication();
if ($_POST['check']!=JUtility::getToken()) {
// First verify (by a Javascript error or other methods) that the form has not been submitted without the validation
if ($_POST['check']=='post') $jAp->enqueueMessage('Please check all the fields of the form, aub.<br/>
If your browser blocks Javascript, then this form will never be successful. This is a security measure.'
// If the check still isn't a valid token, do nothing. This might be a spoof attack or other invalid form submission
return false;

Reference :

back to top