javascript - jquery validation form logic -


i have logic question getting stuck with. need form allow either 1 or other. either text input or checkbox, not both!

}else if(question_pos==7){         if(!($('#test_check').prop('checked')) && $("#test").val()!="0" &&  ($("#test").val() =="" ||                  !number($("#test").val()) ||  number($("#test").val())<0 || number($("#test").val())>20 )){           alert("placeholder?");           return false; 

the inputs:

    <table class="screening" width="100%" cellspacing="5px">     <tr>         <td width="8%" align="right"><input name="test" id="test" type="text" pattern="\d*" size="3" maxlength="2"  value="<%=session.getattribute("test")==null?"":session.getattribute("test")%>"/>      </td>      <td>day(s)</td> </tr>       <tr>         <td width="8%" align="right" valign="top"><input name="test_check" id="test_check" type="checkbox" value="1" <%=(session.getattribute("test_check")!=null && session.getattribute("test_check").equals("1"))?"checked":""%>/></td>         <td width="92%"><label for="test_check">my baby still in hospital</label></td>     </tr>     </table> 

the validation needs make sure there no text input if checkbox checked.

basically, want check if checked property true , if field contains characters. if both conditions met, means form's state invalid.

here's example http://jsfiddle.net/g4q34/

you should avoid calling $("#test") on , over, makes code less readable , inefficient performance-wise. store reference in variable , reuse variable instead.

var checked = $('#test_check').prop('checked'),     val = $("#test").val();  if (checked && val.length) {     //invalid } else {     //the checkbox checked or input contains text, not both } 

here's example own code:

} else if (question_pos==7) {         if($('#test_check').prop('checked') && $("#test").val().length) {             alert("placeholder?");             return false;         } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -