Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation 1

Status
Not open for further replies.

Billybonga

IS-IT--Management
Jun 22, 2002
72
GB
Hi,

I'm unsure if this is a Javascript or ASP question but hopefully someone here can help.

I have a dynamic form that holds a Radio Button.
Depending on the number of records in the database you have have 2-7 options/radio buttons to select from.

From a programming perspective my form code looks like :

Code:
<form onSubmit="return ProgressBar();" action="cast.asp?ID=<%=lAnketID%>" method="post" name="survey" id="survey">

<td colspan="3" class="smalltext"> <input type="radio" name="optSecenek" value="<%=rs("OptionID")%>"> 

 <input type="submit" name="cmdOyVer" value="Submit" class="flatbutton">
</form>

I want to validate if an option has been selected so I have the following Javascript

Code:
<SCRIPT>
function ProgressBar(){

if (document.survey.optSecenek.checked = false){ 
alert("Please select an option");
return false;
}
}
</SCRIPT>

For some reason this doesn't work -- but if it set
optSecenek.checked = true the form upon submit does tell me that there is no option selected, even if there is or isn't one selected.

Is it possible to do this or does having a dynamic radio button prevent it from being done ?

I am able to validate the form in the cast.asp page(when the page is submitted) using :

Code:
if lSecenekID = 0 then  '' If the customer hasn't selected an option then warn them
		%>
                  
                    <p class="smalltext"> <strong><font color="#000000">Please 
                      select an option.</font></strong><br>
                      <a href="<%=request("http_referer")%>">Go back</a> 
                      <%
		response.end
	end if

but i'd prefer to validate the form before posting.

Thanks for your help

Billybonga
 
[1]
>if (document.survey.optSecenek.checked = false){
[1.1] If you check boolean attribute, you're comparing... you need to compare like this.
[tt]if (document.survey.optSecenek.checked =[red]=[/red] false){ //_still wrong_[/tt]
[1.2] But radio button form a collection. Referring them by .optSecenek and check "checked" attribute is a "runtime error". As it is in an onsubmit handler, the handler return immediately and the form will be submitted despite all.

[2] But as said in [1.2] I do not see you loop through recordset part. Make sure your server-side loop is not outside of the form tag. That will make the page a lot of form each with one radio button. Hope it is not the case.

[3] The proper routine will be this.
[tt]
function ProgressBar(){
var crdo=document.survey.optSecenek;
var bselected=false;
for (var i=0; i<crdo.length; i++) {
if (crdo.checked) {
bselected=true;
break;
}
}
if (!bselected) {
alert("Please select an option");
return false;
} else {
//do other things
}
}
[/tt]
 
cheers tsuji - very good explanation and thanks very much for you time on this.

Your proper routine works perfectly ..... thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top