form validation

They have: 117 posts

Joined: Feb 2000

Hello,

I know this must be easy, and maybe I'm just tired, but I can't seem to figure this out. How do I verify input from a select option of a form. If I have something like:

0
1
2
3
4

How do I code "valForm" to check to ensure that some number (other than the slected "0") was chosen? Thanks.

Bob

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Bob,

There's a "Validating Forms" script/tutorial at my site that checks all form elements (except select-multiple (which I forgot but amounts to the same as a select-one). Essentially, you set up a var, loop through the selection list, see if anything is selected and if so, whether it's equal to 0 (if that's a nogood number)

The following will work for more than one selection list, and assumes it is called with a regular button:

function valThis(formObj)
{
var ok2Send = true;
var selOption = 0;
for (i=0; i < formObj.length; i++)

if (formObj.elements[i].type == "select-one")
{
for (j=0; j < formObj.elements[i].length; j++)
if (formObj.elements[i].options[j].selected)
{
selOption = j;
}
if (selOption == 0)
{
ok2Send = false;
alert("You must make a selection")
formObj.elements[i].focus();
}
}

if (ok2Send)
formObj.submit();
}

Vinny

Where the world once stood
the blades of grass cut me still

They have: 117 posts

Joined: Feb 2000

Vinny,

Thanks for the help! I ended up figuring out another way also. I used the following:

if(document.orderForm.day1[0].selected==true &&
document.orderForm.day2[0].selected == true &&
document.orderForm.day3[0].selected == true){
alert("You have not specified any ticket order");
return false;
}

This seems to work fine, or am I missing something that would cause it not to work somehow? Thanks again.

Bob

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Bob,

I'm presuming you have have 3 selection lists (day1...day3)
Are your surfers only IE-users? Because if not, netscape will have a lot to say about the code == all of it bad. The full syntax for a selection list is:

document.formname.selectname.options[document.formname.selectname.selectedIndex].property

Even if you are only concerned about IE, it is not a good idea to get caught in M$'s shortcuts.

Vinny

Where the world once stood
the blades of grass cut me still

They have: 117 posts

Joined: Feb 2000

Vinny,

Thanks. I have both NS and IE viewers, although most are probably IE (through AOL). I always try to write everything so it works on both browsers. The code I gave you does work with both NS and IE. I also tried using:

if(document.orderForm.day1.selectedIndex==0 &&
document.orderForm.day2.selectedIndex==0 &&
document.orderForm.day3.selectedIndex==0){
alert("You have not specified any ticket order");
return false;
}

and this seems to work fine on both NS and IE also. I'm not sure if I totally follow the example you gave in the last post. Can you take what I have above and put it into the syntax you gave? Thanks.

Bob

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Bob,

You were right about the code being NN-friendly.

Question, though, are you playing with 3 selection lists? Because if(this && that && third) means all 3 have to have to be true. Is that what you want? Or, do you want at least one of the selection lists to have an option selected?

Vinny

Where the world once stood
the blades of grass cut me still

They have: 117 posts

Joined: Feb 2000

Vinny,

Yes, I have three selection lists, one for ordering tickets for each performance. I've set each list to have "0" selected to start with. What I want the code to be doing is checking to see if each list still has "0" selected when they hit submit, meaning that they are submitting a ticket order without specifying any tickets for any day. The code, as I have it in my last post seems to be doing what I want. If all three are left at "0", I get an error message telling the viewer he hasn't ordered any tickets. If any one of the lists has a non-zero selection, the order goes through fine. Do you see any problem with the code as it is? Thanks.

Bob

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.