javascript - How can I validate 3 select elements with jQuery? -


i'm trying validate 3 select fields on form, application score keeping application hockey, , recording scoring plays. goal scorer, b primary assist, , c secondary assist. have following validation rules:

  1. a must not empty
  2. b cannot equal or c
  3. c cannot equal or b
  4. if b empty, c must empty

so in other words, a, ab, or abc valid combinations, must unique. disabling submit button applying disabled bootstrap class/html attribute submit button.

in actual code, = awaygoal, b = awaypassist, c = awaysassist

i have following jquery code:

var awaygoal = $("#awaygoal"); var awaypassist = $("#awaypassist"); var awaysassist = $("#awaysassist"); var awaygsubmit = $("#submitawayscore");  $('#awayscore select').change(function() { if(awaygoal.val() != "" && awaypassist.val() != awaygoal.val() && awaygoal.val() != awaysassist.val()) {     if(awaypassist.val() != "" && awaysassist.val() != "") {         awaygsubmit.disablebutton();     }     awaygsubmit.cleardisabled(); } else {     awaygsubmit.disablebutton(); } }); 

this allows unique + c combinations pass through, however, + c same not pass through. each select element has numbers of players involved in game loaded in them. there first option static option, called n/a, code is: <option value="">n/a</option> allows admin have empty primary , secondary assists if not exist.

any appreciated, , way make more efficient welcome starting out jquery. in advance.

since have 3 cases , 3 items can brute force check each condition , or them determine button's disabled status.

jsbin example

var awaygoal = $("#awaygoal"); var awaypassist = $("#awaypassist"); var awaysassist = $("#awaysassist"); var awaygsubmit = $("#submitawayscore");   $('#awayscore select').change(function() {   var = awaygoal.val();   var b = awaypassist.val();   var c = awaysassist.val();   console.log(a,b,c);    if(// case 1:       (a !== "" && b === "" && c === "") ||         // case 2: , b, != b      (a !== "" && b !== "" && c === "" && !== b) ||        // case 3: a, b, c, unique      (a !== "" && b !== "" && c !== "" && !== b && !== c && b !== c) ) {     //awaygsubmit.cleardisabled();   console.log("enable button"); } else {     //awaygsubmit.disablebutton();     console.log("disable button"); } }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -