views:

26

answers:

1

Hi all,

How to do a java script validation in joomla.

thanks in advance for guidance.

A: 

Same was as on the Front-End, by using JHTML::_('behavior.formvalidation');

Here is Joomla's tuturial for adding validation. Only difference is that you will have to copy the CSS for invalid class, it highlights the textboxes when values are not valid.

Also, my personal preference. Instead of coding the <form ... onsubmit="return jsFunction()"> I like to do it like this:

<script type="text/javascript">

window.addEvent('domready', function(){

// Add submit event to the form
$('myForm').addEvent('submit', function(e){
    // Default check
    var isValid = document.formvalidator.isValid(frm);

    // Do additional checks + create message
    var msg  = '';
    if (!isValid){
        msg = 'Some values are not acceptable. Please retry.';
    }else if($('some-other-value').value == 0){
        msg = 'Select IO';
        isValid = false;
    }

    if (!isValid){
        // Stop event
        new Event(e).stop();

        // Somekind of invalid form handler... can do effects in here, etc...
        handleInvalidForm(this, e, msg);
    }

    //
    return isValid;
});


});

</script>
Alex