0
I have a function that validates my fields in the event focusout
, however, now I need to go through all the fields in the submit
of form
, I know I should wear one for
, but I don’t know how.
$("input[type='text'],textarea").focusout(function () {
validar($(this))
});
$("#formcontato").submit(function (event) {
event.preventDefault();
//O for entraria aqui?
});
$('input, textarea').keypress(function (e) {
if (e.which == 13 && validar()) {
request();
return false;
}
});
function request() {
$.ajax({
url: "minha api...",
method: "POST",
data: $(this).serialize(),
dataType: "json",
beforeSend: function () {
//antesEnvio();
},
success: function () {
//sucessoRequest();
},
error: function () {
//erroRequest();
}
});
}
function validar(input) {
var valido = false;
if (input.attr("id") === "email") {
var filtro = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (filtro.test(input.val())) {
input.css("border-color", "green");
} else {
input.css("border-color", "red");
valido = false;
}
}
else if (input.val() === "") {
input.css("border-color", "red");
valido = false;
}
else if (input.val() != "") {
input.css("border-color", "green");
}
return valido;
}
An easy way is to define global variables with the false value for each validation, if they pass they will receive true, at Submit time just check if all are true.
– Felipe Duarte