0

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...thats where I'm hung up. Right now if all fields and buttons are selected then I still get an alert with var alertMsg. Any ideas?

function submitform() {
    var sizeChoice = ""
    var size = document.store.on1.length
    var fontChoice = ""
    var len = document.store.on2.length
    var materialChoice = ""
    var material = document.store.on3.length
    var treatmentChoice = ""
    var treatment = document.store.on4.length
    var a = document.forms["store"]["item_name"].value;
    var alertMsg = "Please Choose a:"
    for(i = 0; i < size; i++) {
        if(document.store.on1[i].checked) {
            sizeChoice = document.store.on1[i].value
        }
    }
    for(i = 0; i < len; i++) {
        if(document.store.on2[i].checked) {
            fontChoice = document.store.on2[i].value
        }
    }
    for(i = 0; i < material; i++) {
        if(document.store.on3[i].checked) {
            materialChoice = document.store.on3[i].value
        }
    }
    for(i = 0; i < treatment; i++) {
        if(document.store.on4[i].checked) {
            treatmentChoice = document.store.on4[i].value
        }
    }
    if(a == null || a == "") alertMsg += "\n" + "Name" + "\n";
    if(sizeChoice == "") {
        alertMsg += "Size" + "\n"
    }
    if(fontChoice == "") {
        alertMsg += "Font" + "\n"
    }
    if(materialChoice == "") {
        alertMsg += "Material" + "\n"
    }
    if(treatmentChoice == "") {
        alertMsg += "Treatment" + "\n"
    } {
        alert(alertMsg)
    };
    return false;
    document.forms["form"].submit();
};
1
  • You have a syntax error down the last if statement, seems like an extra bracket. Also so you know i is global, you might want to define it somewhere. Commented May 30, 2012 at 2:46

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 the return statement blocks any other code that comes after it from running.
1

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

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

to:

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

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

Comments

1

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

Comments

-1
var alertMsg = "";

//....

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

And please add ; to every statement.

2 Comments

alertMsg starts out as "Please Choose a:", so when would it ever evaluate to false?
xdazz, I'm not clear on how this helps. Can you clarify? (Except for the semicolon ;) agree 100%)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.