...

View Full Version : validate both the Radio button and the Select Option box



jennypretty
01-16-2009, 06:41 PM
Hello,

I have been trying to validate both the Radio button and the Select Option box, but it does not work. It didn't show error, only din't do the way I want.

I like to validate like this:

If a user check "Yes" on this Radio button, then the OPTION box is disabled and no need to validate. However,
when I tried the JS code below, the pop up window kept asking for a user to enter the "Seller" name.

I combined both together as:


if (document.frmSelect.sellers.disabled=false && document.frmSelect.sellers.selectedIndex==""

Can anyone please help?

Thank you very much!

html code


Sellers:
<input type="radio" name="SellerName" onClick="document.getElementByID('sellers').disabled=true;" id="et1" value="Yes">Yes
<input type="radio" name="SellerName" onClick="document.getElementByID('sellers').disabled=false;" id="et2" value="No">No
<br>
Names:
<%
strSQL = "SELECT sellerLast, sellerFirst " _
& " FROM tblComboSelect " _
& " WHERE Cars= '" & trim(request.form("Cars")) & "' and DateSold BETWEEN DateAdd('m', -9, Date()) AND Date(); "
Set oRs=Server.CreateObject("adodb.recordset")
oRs.Open strSQL, myConn
if not oRs.eof then %>
<select name="sellers">
<OPTION VALUE = "1" ></option>
<% do until oRs.eof %>
<option value="<%= oRs("sellerLast") %>, <%= oRs("sellerFirst") %>"><%= oRs("sellerLast") %>, <%= oRs("sellerFirst") %></option>
<% oRs.MoveNext
loop %>
</select>
<% else %>
<i>No records found for that car</i>
<% end if %>


JS code:


function chForm() {
var errs = 0
var msgbox = "You forgot the following question (s).\nPlease complete and re-submit. Thanks!\n";
var goon = msgbox
var ctr = 0;

var radCk = false;
for (i=0; i < document.frmSelect.SellerName.length; i++) {
if (document.frmSelect.SellerName[i].checked)
radCk = true; }
if (!radCk) {
msgbox = msgbox + "\n Please check one box";
}
if (document.frmSelect.sellers.disabled=false && document.frmSelect.sellers.selectedIndex=="") {
msgbox = msgbox + "\n Please select seller names";
}

if (document.frmSelect.cars.selectedIndex=="") {
msgbox = msgbox + "\n Please select car type";
}

if (document.frmSelect.q5.value=="") {
msgbox = msgbox + "\n Please add your comments";
}

// If no errors found, then go ahead to submit the form
if (msgbox == goon) {
return true;
} else {

alert(msgbox);
return false;
}
}

jcrypt
01-16-2009, 08:13 PM
Here's an example that will enable/disable a select element based on which radio is checked. This works in FF but you may need to tweak it to get it to work in IE.


<html>
<head>
<script>
function test(rad) {
var sel = document.getElementById('sel');
if (rad.value == 'Yes') {
sel.setAttribute('disabled','true');
} else {
sel.removeAttribute('disabled');
}
}
</script>
</head>
<body>
<form>
<input type="radio" name="rad" value="Yes" onclick="test(this);"/> One<br>
<input type="radio" name="rad" value="No" onclick="test(this);"/> Two<br><br>
<select name="select" id="sel">
<option value="one">One</option>
<option value="two">Two</option>
</select>
</form>
</body>
</html>

itsallkizza
01-16-2009, 08:18 PM
function test(rad)
{
var sel = document.getElementById("sel");
if (rad.value == "Yes")
{
sel.disabled = true;
if (sel.setAttribute) sel.setAttribute("disabled","true");
}
else
{
sel.disabled = false;
if (sel.removeAttribute) sel.removeAttribute("disabled");
}
}

AFAIK, the red works in all browsers though setAttribute is more XHTMLish.

fishluvr
01-16-2009, 11:07 PM
At a glance, I see a few issues...

1) Used assignment (=) in condition, should be: if (document.frmSelect.sellers.disabled==false &&...

2) Radio button onClicks should have lowercase "d" in getElementById().

3) If using getElementById('sellers'), your select element should have an id attribute, not just a name.

jennypretty
01-20-2009, 04:16 PM
Thanks so much.
It worked perfectly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum