View Full Version : JavaScript help for form verification
Pluto
10-14-2004, 11:21 PM
Hi there,
I am having a slight problem with the following checking code, as it is meant to ensure at least one checkbox is checked, if not it pops up an alert. But it displays this even if a checkbox is checked?? I have also included the onClick event applied to the form submit button below. Any help as to why this doesnt work would be fantastic!!
onClick="return verifica(formu)"
<script language="JavaScript">
function verifica(formu)
{
for (m=0;m < formu.anuncios.length; m++)
{
if (formu.anuncios[m].checked == true)
{
return true;
break;
}
}
alert('Debes seleccionar al menos un anuncio');
return false;
}
</script>
Brandoe85
10-14-2004, 11:45 PM
Hmmm something like this works for me...
<html>
<head>
</head>
<body>
<script language="JavaScript">
function verifica()
{
for (m=0;m < document.forms[0].chec.length; m++)
{
if (document.forms[0].chec[m].checked == true)
{
return true;
break;
}
}
alert('Debes seleccionar al menos un anuncio');
return false;
}
</script>
<form>
<input type="checkbox" name="chec"><br>
<input type="checkbox" name="chec"><br>
<input type="button" value="check" onclick="verifica();">
</form>
</body>
</html>
I couldn't really understand formu..hopefully this will help you out :p
Pluto
10-15-2004, 12:15 AM
Hi There
Thanks for the help but it still doesnt seem to work.....does anyone have any other suggestions at all please???
Brandoe85
10-15-2004, 12:22 AM
The code I posted works for me...you just have to put your form name and checkbox name into that code...
<html>
<head>
</head>
<body>
<script language="JavaScript">
function verifica()
{
for (m=0;m < document.yourformname.yourcheckboxname.length; m++)
{
if (document.yourformname.yourcheckboxname[m].checked == true)
{
return true;
break;
}
}
alert('Debes seleccionar al menos un anuncio');
return false;
}
</script>
<form name="yourformname">
<input type="checkbox" name="yourcheckboxname"><br>
<input type="checkbox" name="yourcheckboxname"><br>
<input type="button" value="check" onclick="verifica();">
</form>
</body>
</html>
If that doesn't help you, post your entire code including the form.
Pluto
10-15-2004, 12:29 AM
Hi Brandoe,
Here it is :o) Thank you so much for having a look at this for me, Ive been stuck for the past few hours and its driving me mad!!! :os
<HTML>
<HEAD>
<TITLE>erase adverts</title>
<script language="JavaScript">
function verifica()
{
for (m=0;m < document.formu.anuncios.length; m++)
{
if (document.formu.anuncios[m].checked == true)
{
return true;
break;
}
}
alert('Debes seleccionar al menos un anuncio');
return false;
}
</script>
<link href="../css/immo.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top: 4px;">
<tr>
<td width="145" valign="top"><!--#include virtual="/dev/immo-home.asp"--></td>
<td valign="top"> <div class="subpageHeaders"><%= str_delete %></div>
<% If Request.Form <> "" Then
Response.Write "Pruebas" + "<br>"
Dim i
' for 1 to however many items there are in the form it will execute the code
For i = 1 To Request.Form("anuncios").Count
' Again I'm just writing them out.
Response.Write "sql=DELETE FROM Anuncios WHERE ID=" & CINt(Request.Form("anuncios")(i)) & "<br> "
Response.Write "conex6.Execute(sql)<br>"
Next
Response.Write "<br><center><a href='javascript:history.back()'>" & str_goBack & "</a></center>"
Else%>
<div style="padding: 12px 0px 0px 2px;" id="greenTextSmall"><strong><img src="images/tjl-arrow-green.gif" width="4" height="8" hspace="6"><%= str_deleteWhat %> :</strong></div>
<FORM NAME="formu" METHOD="POST" ACTION="immo-borranun.asp?id=<%=idioma%>">
<%
sql1 = "SELECT * FROM Anuncios WHERE IDContrato = " & IDContrato
sql2 = "SELECT distinct count(*) FROM Anuncios WHERE IDContrato = " & IDContrato
set rdo = conex6.Execute(sql1)
set rdo2 = conex6.Execute(sql2)
If Not rdo2.EOF Then
cuantos=rdo2(0)
Else cuantos=0
End If
If rdo.EOF Then
'Response.Write "SQL: " + sql +"<br>"
Response.Write "<div style='text-align: center;' id='greenTextSmall'><br><br>" & str_noAdverts & "<br>"
Response.Write "<a href='javascript:location.history()'>" & str_goBack & "</a></div>"
Else
par=2
While Not rdo.EOF
%>
<table width="100%" border="0">
<tr>
<% If par MOD 2 = 0 Then %>
<td width="95%" bgcolor="#E8F4DA"><input name="anuncios" type="checkbox" value='<%=rdo("ID")%>'><font face="Tahoma,Verdana" size="1" color="#006600"><strong> Ref: </strong><%=rdo("IDAnuncio")%> <%=rdo("Poblacion")%><%=tBien%> de <%=rdo("Superficie")%> m² con <%=rdo("Habitaciones")%> habitaciones, baņos:<%=rdo("Banyos")%>, aseos:<%=rdo("Aseos")%></font></td>
<%Else%>
<td width="95%" bgcolor="#FFFFFF"><input name="anuncios" type="checkbox" value='<%=rdo("ID")%>'><font face="Tahoma,Verdana" size="1" color="#006600"><strong> Ref: </strong><%=rdo("IDAnuncio")%> <%=rdo("Poblacion")%><%=tBien%> de <%=rdo("Superficie")%> m² con <%=rdo("Habitaciones")%> habitaciones, baņos:<%=rdo("Banyos")%>, aseos:<%=rdo("Aseos")%></font></td>
<%End If%>
</tr>
</table>
<% rdo.MoveNext
par = par +1
%>
<% Wend
End If
conex6.Close
Set conex6 = Nothing
%>
<br>
<br>
<table width="100%" border="0" >
<tr>
<td width="30%" > </td>
<td width="40%" align="center">
<input type="button" class="formButtons" VALUE="Back" onClick = "javascript:history.back()">
<INPUT TYPE="submit" class="formButtons" VALUE="erase" onClick="verifica();">
</td>
<td width="30%" > </td>
</tr>
</table>
</FORM>
<% End If %>
</td>
</tr>
</table>
</BODY>
</HTML>
Brandoe85
10-15-2004, 12:45 AM
Hmm well i'm not to familar with ASP, but as for your javascript code, it seems that when I do not check any checkboxes the alert message pops up like it should, but I think it still trys to submit for form. I would change this line:
<INPUT TYPE="submit" class="formButtons" VALUE="erase" onClick="verifica();">
To this
<INPUT TYPE="submit" class="formButtons" VALUE="erase" onClick="return verifica();">
If it is ASP your having trouble with you could post your code in the ASP forum and someone will be able to help you with that.
Pluto
10-15-2004, 12:49 AM
Thanks for that :o) I will give that a go and see what happens! I may be back tomorrow ;o)
jamescover
10-15-2004, 01:39 AM
if you only have two checkboxes, just give them unique names:
<script type="text/javascript">
<!--
function verifica(formu){
if(!formu.anuncios1.checked && !formu.anuncios2.checked){
alert("Debes seleccionar al menos un anuncio");
return false;
}
}
//-->
</script>
<form name="oForm" action="go.asp">
<input name="anuncios1" type="checkbox" />
<input name="anuncios2" type="checkbox" />
<input name="oSbmt" type="submit" onclick="return verifica(this.form);" />
</form>
-james
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.