No sé nada de Javascript, armé esto leyendo los FAQ.
Funciona bien salvo para el campo email.
Este es mi form:
Código HTML:
<form action=viewtrans_user.php method=post name=form> <table align=center> <tr> <td> From Date : </td> <td> <SELECT NAME="from_month"> <option value="">Month</option> <option value="1">Jan</option> <option value="2">Feb</option> <option value="3">Mar</option> <option value="4">Apr</option> <option value="5">May</option> <option value="6">Jun</option> <option value="7">Jul</option> <option value="8">Aug</option> <option value="9">Sep</option> <option value="10">Oct</option> <option value="11">Nov</option> <option value="12">Dec</option> </SELECT> </td> <td> <SELECT NAME="from_date"> <option value="">Date</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </SELECT> </td> <td> <SELECT NAME="from_year"> <option value="">Year</a> <option value="2000">2000</option> <option value="2001">2001</option> <option value="2002">2002</option> <option value="2003">2003</option> <option value="2004">2004</option> <option value="2005">2005</option> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> </select> </td> </tr> <tr> <td> To Date : </td> <td> <SELECT NAME="to_month"> <option value="">Month</option> <option value="1">Jan</option> <option value="2">Feb</option> <option value="3">Mar</option> <option value="4">Apr</option> <option value="5">May</option> <option value="6">Jun</option> <option value="7">Jul</option> <option value="8">Aug</option> <option value="9">Sep</option> <option value="10">Oct</option> <option value="11">Nov</option> <option value="12">Dec</option> </SELECT> </td> <td> <SELECT NAME="to_date"> <option value="">Date</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </SELECT> </td> <td> <SELECT NAME="to_year"> <option value="">Year</a> <option value="2000">2000</option> <option value="2001">2001</option> <option value="2002">2002</option> <option value="2003">2003</option> <option value="2004">2004</option> <option value="2005">2005</option> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> </select> </td> </tr> <tr> <td> Enter Email (Issuing User) </td> <td colspan=3> <input type="text" name="email"> </td> </tr> <tr> <td colspan=4 align=center> <input type="button" value="Search" OnClick="valida_envia()"> </td> </tr> </table> </form>
Código PHP:
function valida_envia(){
//valido from_date
if (document.form.from_date.value.length==0){
alert("Please enter a valid From Date")
document.form.from_date.focus()
return 0;
}
//valido from_month
if (document.form.from_month.selectedIndex==0){
alert("Please enter a valid From Month")
document.form.from_month.focus()
return 0;
}
//valido from_year
if (document.form.from_year.selectedIndex==0){
alert("Please enter a valid From Year")
document.form.from_year.focus()
return 0;
}
//valido to_date
if (document.form.to_date.selectedIndex==0){
alert("Please enter a valid To Date")
document.form.to_date.focus()
return 0;
}
//valido to_month
if (document.form.to_month.selectedIndex==0){
alert("Please enter a valid To Month")
document.form.to_month.focus()
return 0;
}
//valido to_year
if (document.form.to_year.selectedIndex==0){
alert("Please enter a valid To Year")
document.form.to_year.focus()
return 0;
}
//valido email
if (document.form.email.selectedIndex==""){
alert("Please enter a valid EMail")
document.form.email.focus()
return 0;
}
//el formulario se envia
//document.form.submit();
document.form.submit();
}
</script>
Muchísimas gracias
---Estoy editando
Ya ta, lo encontré. No validaba porque no es un SELECT.
Entonces sería para el campo email:
Código HTML:
//valido email if (document.form.email.value==""){ alert("Please enter a valid EMail") document.form.email.focus() return 0; }