I call my_function
on form submit. The function looks inside a text file for the number entered by the user in my_input
. If this number does not exist, the function pops an alert and should stop form submit.
My problem: the alert is shown, but when I close it, the form gets submitted. What am I doing wrong? Why does my_function
not return false?
function my_function() {
$.get("http://www.example.com/file.txt", function(contents) {
var my_number = document.getElementById("my_input").value;
var hasString = contents.includes(my_number);
if (hasString == false) {
alert('Number does not exist in text file!');
return false;
}
});
}
function function_one() {
var my_number = document.getElementById("my_input").value;
var pattern = /^(0|1|2)\d{5}$/;
if (pattern.test(my_number)) {
$("#cauta_c").val("wait..");
$("#cauta_c").prop("disabled", true);
$("#cauta_c").addClass("my_class");
return true;
}
alert('not valid!');
return false;
}
<form action="https://www.example.com/cgi-bin/script.cgi"
name="whatever"
id="whatever"
method="post"
onsubmit="return function_one() && my_function()"
autocomplete="off"
spellcheck="false"
autocorrect="off"
novalidate="novalidate">