Elisa Elisa - 3 months ago 16
Javascript Question

How to validate form in Magento Javascript as you type

Magento has awesome Javascript validation library, which can be initialized

var myForm= new VarienForm('[your form id]', true);
. However this validation function is triggered when one click on submit button.

Is not there way to validate particular field as you type. For example if I type postal code 2 digit and go to second field, it should instantly validate postal code and show error. As postal code require at least 5 digits.

thanks

Answer

Yes, Magento provide awesome validation library. You can call validation for each field with `validate' method.

For example to validate zip code, you can observe blur event and call validate method.

$('billing:postcode').observe('change', function(e){
    Validation.validate($('billing:postcode'))
})

Notice Validation.validate($('billing:postcode')), this will call validation for postcode field.