I have a form with some fields. If some of the fields are not filled-in - I want to show an alert. If other fields are not filled-in - I want to show an 'soft-alert' and continue submitting the form.
when I use javascript alert window - all works well.
when I use show\hide DIV with my message, the form is submitted before the user manage to read the message.
I tried to use setTimeout
but it does not work.
Any ideas?
similiar questions, but without the 'soft-alert': Onclick validate form, if valid only submit form jQuery Modal that appears on form submit
my JavaScript code:
// - - - - - - - -
// - $popUp alert:
// - - - - - - - -
$('#popUpCloseBtn').click(function() {
$('#popUpMainText').html('');
$('#popUpHeaderText').html('');
$('#popUpContainer').css("display", "none");
});
function popUpShow() {
var popUpObj = document.getElementById("popUpContainer");
popUpObj.style.display = "block";
var topPos = 200; // ($(window).height() / 2) - (popUpObj.offsetHeight / 2);
var leftPos = ($(window).width() / 2) - (popUpObj.offsetWidth / 2);
popUpObj.style.top = topPos + "px";
popUpObj.style.left = leftPos + "px";
}
function showAlert(htmlTitle, htmlText, isErr) {
if (isErr) {
$('#popUpMainText').addClass("popUpTextError").removeClass("popUpTextSuccess");
} else {
$('#popUpMainText').addClass("popUpTextSuccess").removeClass("popUpTextError");
}
$('#popUpMainText').html(htmlText);
$('#popUpHeaderText').html(htmlTitle);
popUpShow();
}
// - - - - - - - -
// - submit form:
// - - - - - - - -
$("#Save").click(function(){
return SaveSubmit();
});
function SaveSubmit() {
var isEmptyField1 = (document.getElementById('f1').value.trim() == '' )
var isEmptyField2 = (document.getElementById('f2').value.trim() == '' )
var strErr = "";
if ( isEmptyField1 ) {
strErr += "<b>WARNING ! YOU CAN NOT CONTINUE.</b>" + "<br />";
strErr += "The following fields are empty, please enter them to continue:" + "<br />";
strErr += "field 1 <br />";
strErr += "field 2 <br />";
// alert(strErr);
var popUpTitle = "Alert";
var popUpText = strErr;
var popUpIsErr = true;
showAlert(popUpTitle, popUpText, popUpIsErr);
return false;
}
if ( isEmptyField2 ) { // 'soft-alert' :
strErr += "FOR INFORMATION ! The following fields are empty: " + "<br />";
strErr += "field 3 \n";
strErr += "field 4 \n";
// alert(strErr);
var popUpTitle = "Alert";
var popUpText = strErr;
var popUpIsErr = false;
showAlert(popUpTitle, popUpText, popUpIsErr);
setTimeout(function() {
var x = " // wait a little, so the user can read the message";
document.form_display.submit();
return true;
}, 20 * 1000); // It does not wait 20 seconds!!!
// do not return false!
}
// If gets here, all is ok . . .
document.form_display.submit();
return true;
}
my HTML code:
<FORM . . .>
. . .
<input type="submit" name="Save" id="Save" value="Enregistrer" >
</FORM>
<div id="popUpContainer" class="popUpContainer">
<div class="popUpHeader" ><span id="popUpHeaderText" ></span></div>
<div class="popUpMain" ><span id="popUpMainText"></span></div>
<div class="popUpFooter" ><input id="popUpCloseBtn" type="submit" value="Close" /></div>
</div>