Validacion en Javascript
Hola, tengo el siguiente codigo para validar un formulario:
<script type="text/javascript"><!--
function validate_form() {
var sel = false;
for(var i=0; i<3; i++) {
if (document.formCounty.radioGroup.checked) {
sel = true;
break;
}
}
if (sel == false) {
alert("You must select: Saturday or Sunday or Both");
return 0;
}
if(document.formCounty.selCounty.selectedIndex==0) {
alert("You must select a County");
return 0;
}
}
// --></script>
function validate_form() {
var sel = false;
for(var i=0; i<3; i++) {
if (document.formCounty.radioGroup.checked) {
sel = true;
break;
}
}
if (sel == false) {
alert("You must select: Saturday or Sunday or Both");
return 0;
}
if(document.formCounty.selCounty.selectedIndex==0) {
alert("You must select a County");
return 0;
}
}
<form id="formCounty" name="formCounty" method="post" action="hola.php" onsubmit="return validate_form()">
<table width="82%" cellpadding="3" cellspacing="0" class="table">
<tr>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radioGroup" value="radio" id="rd1[0]" />
'.$saturday.' </td>
</tr>
<tr>
<td><input type="radio" name="radioGroup" value="radio" id="rd1[1]" />
'.$sunday.'</td>
</tr>
<tr>
<td><label>
<input type="radio" name="radioGroup" value="radio" id="rd1[2]" />
Both</label></td>
</tr>
<tr>
<td>
<select name="selCounty" id="selCounty">
<option>Select county...</option>';
while($row = @mysql_fetch_row($result)) {
echo '<option>'.$row[0].'</option>';
}
echo' </select></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input type=image src="images/buttons/search.gif" width="194" height="19" alt="Search" title="Search"/>
</td>
</tr>
</table>
</form>
Mi problema es que me muestra el alert correspondiente, pero igual se va a la pagina "hola.php", a alguien se le ocurre que estare haciendo mal? Saludos.
<script type="text/javascript"><!--
function validate_form() {
var sel = false;
for(var i=0; i<3; i++) {
if (document.formCounty.radioGroup.checked) {
sel = true;
break;
}
}
if (sel == false) {
alert("You must select: Saturday or Sunday or Both");
return 0;
}
if(document.formCounty.selCounty.selectedIndex==0) {
alert("You must select a County");
return 0;
}
}
// --></script>
function validate_form() {
var sel = false;
for(var i=0; i<3; i++) {
if (document.formCounty.radioGroup.checked) {
sel = true;
break;
}
}
if (sel == false) {
alert("You must select: Saturday or Sunday or Both");
return 0;
}
if(document.formCounty.selCounty.selectedIndex==0) {
alert("You must select a County");
return 0;
}
}
<form id="formCounty" name="formCounty" method="post" action="hola.php" onsubmit="return validate_form()">
<table width="82%" cellpadding="3" cellspacing="0" class="table">
<tr>
<td> </td>
</tr>
<tr>
<td><input type="radio" name="radioGroup" value="radio" id="rd1[0]" />
'.$saturday.' </td>
</tr>
<tr>
<td><input type="radio" name="radioGroup" value="radio" id="rd1[1]" />
'.$sunday.'</td>
</tr>
<tr>
<td><label>
<input type="radio" name="radioGroup" value="radio" id="rd1[2]" />
Both</label></td>
</tr>
<tr>
<td>
<select name="selCounty" id="selCounty">
<option>Select county...</option>';
while($row = @mysql_fetch_row($result)) {
echo '<option>'.$row[0].'</option>';
}
echo' </select></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input type=image src="images/buttons/search.gif" width="194" height="19" alt="Search" title="Search"/>
</td>
</tr>
</table>
</form>
Mi problema es que me muestra el alert correspondiente, pero igual se va a la pagina "hola.php", a alguien se le ocurre que estare haciendo mal? Saludos.
1 respuesta
Respuesta de krlosnow
1