feedback.js it's simple library with beautiful API for validation and sending forms.
Include feedback.min.js use cdn (click for copy to clipboard)
<script src="https://cdn.jsdelivr.net/gh/furashcka/feedback.js@0.1.10/dist/feedback.min.js"></script>
Install via NPM
npm i @furashcka/feedback.js
Install via Yarn
yarn add @furashcka/feedback.js
var el = document.querySelector("form");
var opts = {
focusIncorrectInput: true,
fireSchemaByTurn: true,
blockSubmitWhenFormSending: true,
fireValidateAndAjaxWhenSubmit: true,
resetFormAfterAjax: true,
schema: {},
validationStep: 0,
validationStepChanged: function() {},
ajax: {
loadingClass: "--loading",
url: el.action || location.href,
method: el.method || "POST",
iframePolyfill: "auto",
iframePostMessage: false,
iframeTimeout: 0,
before: function() {},
after: function() {},
success: function() {},
error: function() {},
progress: function() {}
},
validate: {
before: function() {},
after: function() {},
success: function() {},
error: function() {}
}
}
var feedback = new Feedback(el, opts);
var feedback = new Feedback(el, {
schema: {
email: function() {
if (!this.isEmail()) return "Email is not correct!";
}
}
});
// with steps
var feedback = new Feedback(el, {
schema: {
"step-0": {
name: function() {
if (!this.isEmpty()) return "Please, enter your name.";
}
},
"step-1": {
email: function() {
if (!this.isEmail()) return "Email is not correct!";
}
}
}
});
// is recommended use API:
var feedback = new Feedback(el);
feedback.schema({
email: function() {
if (!this.isEmail()) return "Email is not correct!";
}
});
// with steps
feedback.schema("step-0", {
name: function() {
if (!this.isEmpty()) return "Please, enter your name.";
}
});
feedback.schema("step-1", {
email: function() {
if (!this.isEmail()) return "Email is not correct!";
}
});
Input API
var feedback = new Feedback(el, {
ajax: {
url: "//httpbin.org/post",
method: "post"
}
});
// is recommended use API:
var feedback = new Feedback(el);
feedback.ajax({
url: "//httpbin.org/post",
method: "post"
});
feedback.ajax({
success: function(type, xhr) {
switch(type) {
case "ajax.1.0":
console.log("1.0", xhr);
break;
case "ajax.2.0":
console.log("2.0", xhr);
break;
case "iframe":
console.log("iframe", xhr);
break;
}
}
});
feedback.ajax({
error: function(type, xhr) {
switch(type) {
case "ajax.1.0":
console.log("1.0", xhr);
break;
case "ajax.2.0":
console.log("2.0", xhr);
break;
}
}
});
feedback.ajax({
progress: function(percent) {
console.log("progress - " + percent + "%");
}
});
var feedback = new Feedback(el, {
validate: {
error: function(err, inputsArray) {}
}
});
// is recommended use API:
var feedback = new Feedback(el);
feedback.validate({
error: function(err, inputsArray) {}
});
feedback.validate({
error: function(errorMessage, inputsArr) {
var err = "<p>" + errorMessage + "</p>";
var el = inputsArr[0];
el.insertAdjacentHTML("beforebegin", err);
// or with jQuery
$(el).before(err);
}
});
// you can do:
feedback
.schema( /* ... */ )
.validate( /* ... */ )
.ajax( /* ... */ )
/* ... other API */;
// or
feedback.schema( /* ... */ );
feedback.validate( /* ... */ );
feedback.ajax( /* ... */ );
feedback.schema({ /* ... */ });
// with steps
feedback.schema("step-0", {
/* ... */
});
feedback.schema("step-1", {
/* ... */
});
feedback.step("get"); // return options.validationStep;
feedback.step("set", 1); // options.validationStep = 1;
feedback.step("next"); // options.validationStep++;
feedback.step("prev"); // options.validationStep--;
feedback.step("changed", function () {}); // options.validationStepChanged = function () {};
feedback.ajax({ /* ... */ });
sending form:
feedback.ajax();
sending selected inputs:
feedback.ajax(["name", "age"]);
feedback.validate({ /* ... */ });
validating:
if (feedback.validate() === true) { /* ... */ }
feedback.update();
var el = form.querySelector("input[name='email']");
feedback.fireValidateError("Error: wrong email!", [el]);
feedback.resetForm();
feedback = feedback.destroy();
console.log(feedback); // null