I'm trying to protect against empty check boxes. The code works when their empty but also when they aren't so I guess it's not truly working. Also I know there has to be an easier way to check to see if their empty.
First of all, why do you keep repeating document.forms[0] all over the place?
Code:
function confirmsubmit()
{
var f = document.forms[0];
if ( f.visitor_name.value == "" || f.visitor_name.value == "Enter your name")
{
window.alert("You did not enter your name");
return false;
}
...
... similarly throughout ...
...
// last step:
for ( var cb = 1; cb <= 5; ++cb )
{
if ( f["interest"+cb].checked ) return true;
}
alert("You must check at least one interest";
return false;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I had a template from something else so I copied and pasted a lot of it and when I started doing new code I just kept doing it I'll use a variable next time thanks for all the help.
Using document.forms[0] ties the JavaScript directly to the HTML so that adding another form to the page (such as if you decide to add a search box into the header of each page) will instantly break your JavaScript.
Using document.getElementsByTagName('visitor_name')[0].form would be a better alternative because it would at least mean that the JavaScript isn't affected unless another form with that same field name in it is added.
You don't need to give the form a name or id or even refer to it at all. You could just give the text boxes, check boxes etc an appropriate id or name and refer to them directly when validating them in your js.
Yes, but why not simply insist that every <form> have an ID? ID's *MUST* be unique, so there's not even the possibility of a duplicated field name.
My suggestion was made on the basis of not amending the HTML for the current form. Using an id for accessing the form from JavaScript is certainly the better option.
Where you have an id on any field within the form you don't need one on the form tag itself as any field within the form provides easy access to the entire form.
My suggestion was made on the basis of not amending the HTML for the current form. Using an id for accessing the form from JavaScript is certainly the better option.
Where you have an id on any field within the form you don't need one on the form tag itself as any field within the form provides easy access to the entire form.
Very true, but if the form is to be submitted to a server-side script then the form elements must have names (in addition to ids if present). Like Old Pedant I see no real need to assign ids to form elements.
__________________
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.
Very true, but if the form is to be submitted to a server-side script then the form elements must have names (in addition to ids if present). Like Old Pedant I see no real need to assign ids to form elements.
But id's make it easier to reference the form elements in the css if you want to style them in a specific way.
So what you *NEED* to do comes down to preference because you don't *NEED* to reference the form at all either - by name and/or id or forms[x] - to access the form elements.
But id's make it easier to reference the form elements in the css if you want to style them in a specific way.
So what you *NEED* to do comes down to preference because you don't *NEED* to reference the form at all either - by name and/or id or forms[x] - to access the form elements.
Yes, bullant. It is (as always) a case of horses for courses, or doing what is most suitable/appropriate/convenient in any particular situation.
That is not exactly the same as personal preference.
__________________
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.
....doing what is most suitable/appropriate/convenient in any particular situation.
That is not exactly the same as personal preference.
In this case it is exactly the same as personal preference because no way of accessing form elements suggested in this thread is better than the other. So which way you or I or old pedant or felgall think is best comes down to each of our personal preferences.
Being retired I no longer have to put up with other peoples' views on how things should be done. I can now freely do what I think is best when I have more than 1 option