PDA

View Full Version : Javascript question


Wyatt
2006-04-04, 14:41
I'm working on a Javascript form validator, and I have a question. One of my form inputs is a set of radio buttons. How do I check to see that a selection has been made? Or, more specifically, how can I check to see that a certain selection has not been made? Here's what I have:

else if (document.joinform.paytype.value == 1)
{alert("You must select a payment type");
return false;}


Where a paytype value of 1 indicates the default value of "Select a payment type". Selection 2 is by check, and selection 3 is by PayPal.

<input type="radio" name="paytype" id="1" value="1" checked="checked" /><label for="1">Select a Payment Type</label><br />
<input type="radio" name="paytype" id="2" value="2" /><label for="2">Pay In Person</label><br />
<input type="radio" name="paytype" id="3" value="3" /><label for="3">PayPal</label>


As it is, the rest of my validator works except that one section. I thought I had everything right, but it doesn't work. It goes ahead and submits the form (which it should not do without a proper payment type selected).

Wyatt
2006-04-04, 14:51
Nevermind, I figured it out. For what it's worth, I'll post what I did in case somebody with a similar problem stumbles across this thread later on.

I created a variable called "payment" with an initial value of 1. I then added an onclick event to my radio buttons to change the value of payment. Finally, I had my validator simply check the value of payment.

var payment = 1
<input type="radio" name="paytype" id="1" value="1" onclick="payment=1" checked="checked" /><label for="1">Select a Payment Type</label><br />
<input type="radio" name="paytype" id="2" onclick="payment=2" value="2" /><label for="2">Pay In Person</label><br />
<input type="radio" name="paytype" id="3" value="3" onclick="payment=3" /><label for="3">PayPal</label>
else if (payment == 1)
{alert("You must select a payment type");
return false;}