12

In magento1 we can check validation like this:

var formToValidate = $('form-validate');
    var validator = new Validation(formToValidate);
    if(validator.validate()) {
        // Here we can code after validation.
    }

but in magento2 i want to check all validation but don't want to submit form after success. so how can I perform with magento2 form?

2 Answers 2

23

Try this:

require(["jquery"], function ($) {
    $(document).ready(function () {
        $('#my-button-name').click(function () { // The button type should be "button" and not submit
            if ($('#form-name').valid()) {
                alert("Validation pass");
                return false;
            }
        });
    });
});

Hope this helps!

1
  • 1
    works like charm.!! Commented Oct 21, 2016 at 10:44
4

You can use solution where you have data-mage-init validation

  require([
        'jquery',
        'jquery/ui'
    ], function($){
        $(".MYbuttonClass").on("click", function() {
            
           if ( $('.form-Class').validation() && $('.form-Class').validation('isValid') === true) {
                //Submit form    
                $('.form-Class').submit();
  
            }

        });

    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.