PHP Magento Tips

How to create custom Validation Rule (Validation Method) with jQuery Validation Plugin in Magento

Rate this posting:
{[['']]}

You can easily create your own validation class and create your own rule for that class in a jQuery Validation way which should work well with Magento.

Here is the solution, how you can achieve this.

Suppose in some case, you need your custom jQuery validation and for other case it’s not.

For that, you can apply your own custom class with if condition and then write Custom rule for that.

Have a look on below code:
var country = $F('country');
if (country == 'CA') {
    $('postcode').addClassName('min-length-ca');
} else {
    $('postcode').removeClassName('min-length-ca');
}

Here, in country drop down list if your country is equal to 'US' then it will add your own custom class.

After that add your validation rule for that Postcode field validation.
Validation.add('min-length-ca','Please enter correct Zip Code.',function(v){
 var regexp = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
    if(v.search(regexp) == -1)
 { 
    return false;
 } else {
    return true;
 }
           
});

Above validation method work well for magento even in onepage checkout Page.

For that, you have to add same validation code in opcheckout.js file at the beginning of your Billing and Shipping Method's Save function in opcheckout.js file.

Note: You can find more about Regular Expression Validation Rule Specifically for US and Canada Country from Here.
Tag : Magento
1 Comments On "How to create custom Validation Rule (Validation Method) with jQuery Validation Plugin in Magento"

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Typo3, kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on Typo3. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com


Back To Top