Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 317
Elisa Avatar asked Jun 13 '14 09:06

Elisa


People also ask

How JavaScript form can be validated?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.

How do you validate a form in regex?

You can use regular expressions to match and validate the text that users enter in cfinput and cftextinput tags. Ordinary characters are combined with special characters to define the match pattern. The validation succeeds only if the user input matches the pattern.


1 Answers

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.

like image 173
Krishna Sunuwar Avatar answered Oct 28 '22 14:10

Krishna Sunuwar