Initiate the Joomla validate.js
In order to include validation, call the validate.js as shown below.
JHTML::_('behavior.formvalidation');
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) {
regex=/^\d{4}(-\d{2}){2}$/;
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.','error');
// 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 : http://docs.joomla.org/Form_validation



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