At last, I was able to achieve this:
function validate(data_to_validate, url, section)
{
var datastr = "section=" + section + "&data=" + data_to_validate;
return new Promise(function(resolve, reject) {
$.ajax({
type:'GET',
url:url,
data:datastr,
success:function(response) { resolve(response); },
error:function(response) { reject(response); }
});
});
}
async function verify_submission()
{
init(); // initializes some global variables
personal_errors_count = 0; // count errors
var dni_ctl = document.getElementById("frm_employee_dni");
var dni = dni_ctl.value;
dni_ctl.style.borderColor = "black";
dni_ctl.style.backgroundColor = "none";
const status = await validate(dni, validation_url, "dni")
.then(output => validateStatus(dni_ctl, output)) // validateStatus() updates color and form control title
.catch(error => validateStatus(dni_ctl, "error"));
$.when(status).done(console.log("Errors: " + personal_errors_count));
return false; // set for testing purposes, but always ignored.
}
Whenever the onsubmit
event occurs, async function check_submit()
is triggered. The validate(...)
function returns a promise
, which is handled with .then()
and .catch()
to manage both outcomes. However, even with incorrect fields, the form gets submitted.
I suspect that the form always gets submitted due to faulty code somewhere, but identifying it has been challenging.
Appreciate any assistance!