47 lines
990 B
JavaScript
47 lines
990 B
JavaScript
$(function() {
|
|
FormValidator = {};
|
|
|
|
FormValidator.onError = function(str)
|
|
{
|
|
alert(str);
|
|
console.log(str);
|
|
};
|
|
|
|
FormValidator.validate = function(callback)
|
|
{
|
|
var pass = 1;
|
|
|
|
$("[fv-regex]").each(function(i, x) {
|
|
|
|
if (pass == 0)
|
|
return;
|
|
|
|
x = $(x);
|
|
|
|
let input = x.val();
|
|
let regex = new RegExp(x.attr("fv-regex"), "g");
|
|
|
|
if (!input.match(regex))
|
|
{
|
|
x.addClass("border-danger");
|
|
x.focus();
|
|
x.select();
|
|
|
|
let warning = x.attr("fv-warning") ?? "Please correct the highlighted input and try again";
|
|
|
|
FormValidator.onError(warning)
|
|
|
|
pass = 0;
|
|
return;
|
|
}
|
|
|
|
if (pass == 1)
|
|
if (typeof callback === "function")
|
|
callback();
|
|
|
|
x.removeClass("border-danger");
|
|
});
|
|
|
|
};
|
|
});
|