I have a jquery to validate my drop down. if ‘yes’ on radio box i can select from dropdown. if ‘no’ on radio box dropdown is greyed out.
If a both radio boxes are ‘yes’ and value in both dropdowns are ‘please select’ and submit button is clicked, a validation error occurs and DOES NOT redirect to index.html.
If one radio box is at ‘yes’ & dropdown ‘please select’ and the other at ‘no’ with value ‘red’ from dropdown and submit button is clicked a validation error occors and redirect to index.html. Obviously this defeats the purpose of a validation.
I hope someone can help as its driving me mad.!
I have narrowed it down to this being the probem:
});
$("input:submit").click(function(){
var retVal = false;
$.each([1, 2], function(i, val){
retVal = (validate(val) || retVal);
});
return retVal;
});
If I understand correctly, you have a
validate()function (which you don’t show) which validates the specified item and returnstrueif OK orfalseif invalid. On submit you want to callvalidate()for several items, and if any of them returnfalsereturnfalseto the submit (conversely, returntrueonly if all the validations aretrue.The way your existing code sets
retVal, once it becomestrueit will staytruebecause any boolean result fromvalidate()ORed withtruewill betrue. Try this instead:This starts with a default return of
true, and then changes it tofalseifvalidate(val)returnsfalse. Using&&instead of||then means that onceretValisfalseit will stayfalse. In my opinion it would be clearer to just get rid of the&&altogether and say: