javascript - How can I use the JQuery Validation Plugin to validate this form that have this specific constraint? -


this question has answer here:

i pretty new in jquery , have following problem related form validation.

so think not write validator myself use jquery validation plugin, one: http://jqueryvalidation.org/

from have understand provide me built in required() method make specific field required (if user don't insert value field error message shown).

ok...my problem in form have this:

<form method="post" action="consultazionericercaform" id="consultazionericercaform">      <div class="row">         <div class="col-md-4">              <label style="display: block;">regioni:</label>             <select name="regioneselezionata" id="selreg">                 <option value="-">--selezionare una regione--</option>                 <option value="ab">abruzzo</option><option value="ba">basilicata</option><option value="ca">campania</option><option value="cl">calabria</option><option value="ee">regione estera</option><option value="em">emilia romagna</option><option value="fr">friuli-venezia giulia</option><option value="la">lazio</option><option value="li">liguria</option><option value="lo">lombardia</option><option value="ma">marche</option><option value="mo">molise</option><option value="pi">piemonte</option><option value="pu">puglia</option><option value="sa">sardegna</option><option value="si">sicilia</option><option value="to">toscana</option><option value="tr">trentino-alto adige</option><option value="um">umbria</option><option value="va">valle d' aosta</option><option value="ve">veneto</option>             </select>         </div>           <div class="col-md-4">             <label style="display: block;">province:</label>             <select name="provinciaselezionata" id="selprov"><option value="">--selezionare una provincia--</option></select>         </div>           <div class="col-md-4">         </div>      </div>      <div style="margin-top: 30px;" class="row">         <div class="col-md-4">             <label style="display: block;">codice meccanografico:</label>             <input type="text" value="" name="codmec" id="selcodmec">             </div>              <div class="col-md-4">                 <label style="display: block;">tipologia progetto:</label>                 <select name="tipologiaprogettoselezionato" id="seltipologiaprogetto">                     <option value="-">--tipologia--</option>                     <option value="wifi">wifi</option><option value="lim">lim</option><option value="altro">altro</option><option value="classi 2.0">classi 2.0</option><option value="scuola 2.0">scuola 2.0</option><option value="csd">csd</option>                 </select>              </div>               <div class="col-md-4">                 <label style="display: block;">stato progetto:</label>                 <select name="statoprogettoselezionato" id="selstatoprogetto">                     <option value="-">--stato progetto--</option>                     <option value="da compilare">da compilare</option><option value="in lavorazione">in lavorazione</option><option value="da validare">da validare</option><option value="validato">validato</option><option value="rendicontato pregresso">rendicontato pregresso</option>                 </select>              </div>          </div>          <div style="margin-top: 30px;" class="row">             <div class="col-md-12">                 <input type="submit" value="cerca">             </div>         </div> </form> 

as can see contains field of have id="selreg" , id="selcodmec".

so validation focused on previous 2 fields , in particular form can considered valid if field having id="selreg" or field having id="selcodmec" have value setted. if @ least 1 of these 2 fields valorized user form considered valid.

i think in case can't use required() method because seems me required() method refered on specific field , don't implement previous logic.

how can solve problem? can use jquery validation plugin purpose?

tnx

i use in 1 of project jquery validation well.

try add own validation rule:

$.validator.addmehtod('oneorother',function(value) {   if(!$("#selreg").val() && !$("#selcodmec").val())     return false;   else     return true; }); 

assign validation field:

$("#consultazionericercaform").validate({   ...   rules: {     regioneselezionata: {       oneorother: true     }    ...   } }); 

i hope, understood question correctly.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -