To validate radio buttons you must loop through them to identify which one is checked.
Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.
What if your user types "Mickey Mouse" into the renewal date field? Surely the renewal date must be validated as a future date in the correct format.
Look at the difference between || and && (and) and their negatives expressed by !=.
St.Paul cavorted to Christianity, he preached holy acrimony which is another name for marriage.
- Pupil's answer to Catholic Elementary School test.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
var val = "";
var op = document.getElementById("opt");
for (var i = 0; i<op.length; i++) {
if (op[i].checked) {
val = op[i].value;
}
if (val == "") { // no radio selected
alert ("Please make your selection");
return false;
}
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
function notEmpty(){
var emailField = document.getElementById('emailaddress');
var optField = document.getElementById('opt');
var firstnameField = document.getElementById('firstname');
var renewaldateField = document.getElementById('renewaldate');
if(emailField.value == "" || optField.value == checked || firstnameField.value =="" || renewaldateField.value =="") {
alert("Please fill in the form");
return false;
} else if(optField.checked == "") {
alert("Please select your vote");
return false;
} else if(emailField.value != "" || optField.value != checked || firstnameField.value != "" || renewaldateField.value != "") {
} return true;
var val = "";
var op = document.getElementById("opt");
for (var i = 0; i<op.length; i++) {
if (op[i].checked) {
var val = op[i].value;
}
if (val == "") {
alert ("Please make your selection");
return false;
}
}
}
only the first name and email textboxes validate
Well, you have return false and return true both earlier in the function, so the radio validation is never reached. And you seem to be trying to validate the radios twice.
It is best to start by validating only one field at a time, get that working (with proper validation, not the feeble == ""), then add another, and so on.
If the validation fails, it is best to give a specific message to the user indicating what is wrong, not some generalised message such as "Please fill in the form".
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
I do not wish to be impolite, but you are supposed to be a Senior Coder with over 1400 posts. Surely you ought to know how to validate radio buttons?
And as you might expect there are many examples if you Google.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.