magento 2: trigger js form validation without submit
was able to figure this out by referencing:
module-checkout-agreements/view/frontend/web/js/model/agreement-validator.js
After including mage/validation
you have to pass argument isValid
to mage.validation in order to invoke the validation
Full code looks like
require([
'jquery',
'mage/validation'
], function($){
var dataForm = $('#form-validate');
var ignore = null;
dataForm.mage('validation', {
ignore: ignore ? ':hidden:not(' + ignore + ')' : ':hidden'
}).find('input:text').attr('autocomplete', 'off');
$('button#my-button').click( function() { //can be replaced with any event
dataForm.validation('isValid'); //validates form and returns boolean
});
});
You can also replace argument with clearError
to remove validation error messages
You can 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;
}else{
alert("Validation failed");
return false;
}
});
});
});
Hope this may help you!