Skip to content Skip to sidebar Skip to footer

Javascript Form Submission After Validation

This script functions mostly how I would like it to: alert when a radio checkbox has not been selected. However, if all buttons are selected I need it the form to be submitted...th

Solution 1:

You have a return before the form submit. That may be part of the problem.

Also, you are missing an else on the final if statement.

Solution 2:

You're returning false regardless of your validation. Change the end of your code from:

if(treatmentChoice == "") {
    alertMsg += "Treatment" + "\n"
} {
      alert(alertMsg)
};
returnfalse;
document.forms["form"].submit();

to:

if(treatmentChoice == "") {
    alertMsg += "Treatment" + "\n"
}
if(alertMsg.length > 16) {
    alert(alertMsg);
    returnfalse;
} else {
    document.forms["form"].submit();
}

The length check checks the final value length of alertMsg against what you originally set it to.

Solution 3:

Your returning before the form submit line, so it will never be called.

Solution 4:

var alertMsg = "";

//....if(alertMsg) {
    alert("Please Choose a:" + alertMsg);
} else {
    document.forms["form"].submit();
}

And please add ; to every statement.

Post a Comment for "Javascript Form Submission After Validation"