I have a function that validates my fields in the focusout
event, however, I now need to go through all the fields in submit
of form
, I know I should use for
, but I do not 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;
}