Validating Radio / Option boxes
I'm writing an asp page that's using vbs, js and a access DB.
It's using VBS to build a table of 'clients' from the DB
The first field in each row is a radio/options button who's value is assigned the clientID from the DB
I need to make sure the user has selected one of the option boxes when they hit the Submit button.
But it's not working, I keep getting 'undefined' for frmAdmin.optClientInfo.value
I'm using similar code to validate text boxes and it works great.
Can someone show me the problem here?
Code:
<HEAD>
<title>Admin Page</title>>
<SCRIPT language="JavaScript">
function ValidateData()
{
var sMsg = ""
// The next line is debugging test, but it returns 'undefined'
alert(frmAdmin.optClientInfo.value);
if (frmAdmin.optClientInfo.value == "")
{
sMsg = "Please select an account."
}
else if (frmAdmin.optAction.value == "")
{
sMsg = "Please select an Action."
}
if (sMsg != "")
{
alert(sMsg);
event.returnValue = false
}
//else the form will be submitted automatically
}
</SCRIPT>
</HEAD>
' I'm skipping a lot of code here
' it's using VBS to build a table of 'clients' from an access DB
' the first field in each row is a radio/options button who's value is assigned the clientID from the DB
<FORM NAME="frmAdmin" METHOD="post" OnSubmit="ValidateData()" ACTION="AdminEdit.asp">
'This next line is nested inside a loop for each 'client'
<td><INPUT TYPE="Radio" NAME="optClientInfo" VALUE=<%=lClientId%>></td>
Re: Validating Radio / Option boxes
try checking the ".checked" property. :) loop through the radio buttons (using the name) and if none of the .checked properties hold "true", then not a single one was selected. :) hope this helps.
Re: Validating Radio / Option boxes
Just change two lines like:
if (!frmAdmin.optClientInfo.checked) {
else if (!frmAdmin.optAction.checked) {
and your code will work fine
Re: Validating Radio / Option boxes
No luck.
Using Checked creates the alert even when one of the option buttons is selected.
And the following code still returns 'undefined'.
I get the impresion that the JS just isn't seeing the optClientInfo's
Code:
alert(frmAdmin.optClientInfo.checked);
Maybe it's because they are an array?
How would you loop through them when you don't know how many will be on the page?
Re: Validating Radio / Option boxes
I just tried this and it works,
Code:
alert(frmAdmin.optClientInfo(1).checked);
so I just need to know how to loop through them using js.
Can someone show me?
Re: Validating Radio / Option boxes
i have just changed your code and its working fine.
vb Code:
<HEAD>
<title>Admin Page</title>>
<SCRIPT language="JavaScript">
function ValidateData()
{
var sMsg = ""
// The next line is debugging test, but it returns 'undefined'
alert(frmAdmin.optClientInfo.value);
alert(frmAdmin.optClientInfo.checked);
if (!frmAdmin.optClientInfo.checked)
{
sMsg = "Please select an account."
}
else if (frmAdmin.optAction.checked == "")
{
sMsg = "Please select an Action."
}
alert(sMsg)
if (sMsg!= "")
{
alert(sMsg);
event.returnValue = false
}
//else the form will be submitted automatically
}
</SCRIPT>
</HEAD>
' I'm skipping a lot of code here
' it's using VBS to build a table of 'clients' from an access DB
' the first field in each row is a radio/options button who's value is assigned the clientID from the DB
<FORM NAME="frmAdmin" METHOD="post" OnSubmit="ValidateData()" ACTION="AdminEdit.asp">
'This next line is nested inside a loop for each 'client'
<INPUT TYPE="Radio" NAME="optClientInfo" VALUE=<%=lClientId%>>
<INPUT type="button" onclick="ValidateData()" value="test">
</BODY>
</HTML>
will you pass on the complete code?
Re: Validating Radio / Option boxes
Thx vksingh24,
But I tried those changes to the ValidateData function and they didn't work.
I think what you missed is that a separate optClientInfo is made for each Client in the DB.
So we're dealing with a control array, and not a single radio/ options button(see my last post)