I've figured out how to fill a date form with a random date if it is left unfilled within a certain period, but the field is filled only if I cancel the submission.
const handleSubmit = (event) => {
if(Date === ""){
let var1 = Math.floor(Math.random() * (30 - 1) + 1);
let var2 = Math.floor(Math.random() * (12 - 6) + 6);
if(var1.toString().length === 1){
var1 = "0" + var1;
}
if(var2.toString().length === 1){
var2 = "0" + var2;
}
setDate(var1 + "/" + var2 + "/" + "2021");
}
event.preventDefault();
const answer = window.confirm("Please confirm you have entered your information " +
"correctly.\n" +
"\nEmail: " + email +
"\nFirst Name: " + Name +
"\nLast Name: " + LastName +
"\nGender: " + Gender +
"\nAMKA: " + AMKA +
"\nDate: " + Date);
if (answer) {
const url = "...";
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, Name, LastName, Gender, AMKA, Date })
};
fetch(url, requestOptions)
.then((response) => console.log("Submitted successfully", requestOptions.body))
.catch((error) => console.log("Form submit error", error));
} else {
console.log("Your submission was not sent.");
}
};
Turned out it was a rookie mistake, I just pushed up my code which handles the date randomization.