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:
- a must not empty
- b cannot equal or c
- c cannot equal or b
- 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.
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
Post a Comment