magento2: how validation work in magento2
-
12-04-2021 - |
Question
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?
Solution
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!
OTHER TIPS
You can use solution where you have data-mage-init valition
require([
'jquery',
'jquery/ui'
], function($){
$(".MYbuttonClass").on("click", function() {
if ( $('.form-Class').validation() && $('.form-cart').validation('isValid') === true) {
//Submit form
$('.form-Class').submit();
}
});
});
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange