10 Jan 2014

Magento :Custom Form With Default Validation

/* Magento :Custom Form With Default Validation */
When I was new in magento.I had found in registration form of customer there are some default validation . I was really getting wondered that where that is ? Now I don’t like you to be wondered .That means there are some default classes available in magento which are present in validation.js file.So please the following details then you will be understood.So I have taken the below html as an example html.

I have a custom form in view.phtml having following code:

Note : The classes 'required-entry' and 'validate-email' ? These both tell the classes to apply certain validation rules on the input fields. If any of the validation checks fail, the form will not be submitted and the user will be alerted to the errors.
Now I would like to validate my form “myForm” when I click on submit button First, I need to create a Form (js/varien/form.js) object to represent my form.

//this below code is the javascript code

The first parameter, in this case myForm, is the ID of my form. The second parameter defines whether or not the first input field in the form should steal the cursor focus. If set to true, the cursor will automatically be moved into the first input field and any user input will be entered into this field. You can disable this functionality by setting the second parameter to false.

Related Links :

1)Magento : List Of Prototype Javascript Validation Classes With Error Message
2)Magento : Submit Custom Form With Magento’s Built-In AJAX With Validation