0

I am trying to verify a form that has multiple check boxes, and when the "other" check box is checked and the "other_text" input has nothing in it. I need an error to fire asking to input the other text. here is the current verification that does not work with my "other" check box.

}else if(question_pos==24){    
        if((!$('#followup_six_45_physician').prop('checked') && !$('#followup_six_45_pharm').prop('checked')  && !$('#followup_six_45_nurse').prop('checked')  && !$('#followup_six_45_none').prop('checked')  && !$('#followup_six_45_other').prop('checked')) || 
        ($('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "" )){

            if(( $('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "")){
                alert("You selected \"Other\" for race, please fill in what other race you consider yourself to be.");
                return false;
            }else{
                alert("Please select an answer.");
                return false;
            }
        }else{ 
            question_pos+=1;
            showContent(question_pos,"right");
            progress(93, $('#progressBar'));
            return true;
        }

this is the problem...

if(( $('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "")){
                    alert("You selected \"Other\" for race, please fill in what other race you consider yourself to be.");
                    return false;

Here is html

<div id="area24">
   <div id="bl_blue">

        <form name="form24" action="handler_2.jsp" onsubmit="return verifyIt();" method="post" style="padding-bottom:20px;">
        <input type="hidden" id="from" name="from" value="baseline_01" />
        <input type="hidden" id="direction" name="direction" value="" />
  <h3>Advice from:</h3>
        <table class="screening" width="100%" cellspacing="5px">
          <tr>
            <td width="8%" align="right"><input name="followup_six_45_physician" id="followup_six_45_physician" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_physician")!=null && session.getAttribute("followup_six_45_physician").equals("1"))?"checked":""%>/></td>
            <td width="92%"><label for="followup_six_45_physician">a physician</label></td>
        </tr>
          <tr>
            <td width="8%" align="right"><input name="followup_six_45_pharm" id="followup_six_45_pharm" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_pharm")!=null && session.getAttribute("followup_six_45_pharm").equals("1"))?"checked":""%>/></td>
            <td width="92%"><label for="followup_six_45_pharm">a pharmicist</label></td>
        </tr>
          <tr>
            <td width="8%" align="right"><input name="followup_six_45_nurse" id="followup_six_45_nurse" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_nurse")!=null && session.getAttribute("followup_six_45_nurse").equals("1"))?"checked":""%>/></td>
            <td width="92%"><label for="followup_six_45_nurse">a nurse</label></td>
        </tr>
          <tr>
            <td width="8%" align="right"><input name="followup_six_45_other" id="followup_six_45_other" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_other")!=null && session.getAttribute("followup_six_45_other").equals("1"))?"checked":""%>/></td>
            <td width="92%"><label for="followup_six_45_other">Other:</label> <input name="followup_six_45_other_text" type="text" value="<%=session.getAttribute("followup_six_45_other_text")==null?"":session.getAttribute("followup_six_45_other_text")%>"/></td>
        </tr>
          <tr>
            <td width="8%" align="right"><input name="followup_six_45_none" id="followup_six_45_none" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_none")!=null && session.getAttribute("followup_six_45_none").equals("1"))?"checked":""%>/></td>
            <td width="92%"><label for="followup_six_45_none">I used none of these</label></td>
        </tr>
        </table>
            </form>

            </div></div>

So far, I get an error when the other checkbox is checked, but when I add text to the input, the error still fires. I dont know what to do to make the error only fire when there is no text in the "followup_six_45_other_text" and the "followup_six_45_other" is checked.

Any help would be appreciated, for I am completely stuck right now.

3
  • 1
    See if you can create a simplified demo at jsfiddle.net Commented Apr 17, 2013 at 18:07
  • you should use jquery validation plugin. It can take care of such validation fairly easily Commented Apr 17, 2013 at 18:09
  • Can you share the HTML? Would help understand the div structure/ids. Commented Apr 17, 2013 at 18:10

1 Answer 1

1

Looks like you are querying by id for followup_six_45_other_text, but the id is not actually set(Name is set, id is not, you are querying by id).

     <td width="92%"><label for="followup_six_45_other">Other:</label> <input name="followup_six_45_other_text" type="text" value="<%=session.getAttribute("followup_six_45_other_text")==null?"":session.getAttribute("followup_six_45_other_text")%>"/></td>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh damn! I cant believe it was so simple, THANK YOU!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.