how to validate both check box and textbox field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    how to validate both check box and textbox field

    hi,
    i created one jsp page..in it i used check boxes as well as textboxes..i need to check both the check boxes and textboxes..i tried it but it is validating only the check box not the textbox..here is the code can anyone help me out...

    login.jsp

    Code:
    <td><font color="#800000" size=""><b>Low-Bal</b></font></td>
                            <td><input type="checkbox" name="balrdys" value="F,">Fresh A/c <input type="text" name="balrdys" size="1"><br>
                                <input type="checkbox" name="balrdys" value="A,">Renewal   <input type="text" name="balrdys" size="1"><br>
                                <input type="checkbox" name="balrdys" value="NO">No<br>
                            <td></td>
    login.js--javascript code

    Code:
    var chkBalrdays = document.getElementsByName('balrdys');
        var hasBalrdaysChecked = false;
        for (var j = 0; j < chkBalrdays.length; j++)
        {
            if (chkBalrdays[j].checked)
            {
                hasBalrdaysChecked = true;
                if (document.frm.balrdys.value == "")
                {
                    alert("Enter Bal-R-Days");
                    document.frm.balrdys.focus();
                    return (false);
                }
                break;
            }
        }
        if (!hasBalrdaysChecked)
        {
            alert("Please select at least one Low BalRDays");
            return false;
        }
    Thanx in advance,
    nd waiting for the reply
    madhu.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s because text boxes do not have a checked property, which is evaluated as false.

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      hi,
      i didnt get your point...i changed the following in javascript..eve nthough i m not getting...can u tell where to modify to get validated.

      Code:
      var chkBalrdays = document.getElementsByName('balrdys');
          var hasBalrdaysChecked = false;
          for (var j = 0; j < chkBalrdays.length; j++)
          {
              if (chkBalrdays[j].checked)
              {
                  hasBalrdaysChecked = true;
                  break;
              }
          }
          if (!hasBalrdaysChecked)
          {
              alert("Please select at least one Low BalRDays");
              return false;
          }
          if (document.frm.balrdys.value == ""){
              alert("Enter Balance");
              document.frm.balrdys.focus()
              return (false);
          }
      Thanx,
      madhu

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        this time it’s different. as demo do
        Code:
        alert(document.frm.balrdys.value);
        it should give you [object HTMLCollection]. such an object (to be more precise, this is a kind of Array) doesn’t have a value property. thus it should always enter the condition’s statement.

        my recommendation: use a different name for the checkboxes and the textboxes, so you can loop over each of them separately.

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          Originally posted by Dormilich
          this time it’s different. as demo do
          Code:
          alert(document.frm.balrdys.value);
          it should give you [object HTMLCollection]. such an object (to be more precise, this is a kind of Array) doesn’t have a value property. thus it should always enter the condition’s statement.

          my recommendation: use a different name for the checkboxes and the textboxes, so you can loop over each of them separately.
          hi,
          i changed my code as follows...the problem is it is not checking the checkboxes..it is only taking text boxes...im not getting where the problem occurs..here is the code...

          login.jsp

          Code:
          <td>
              <input type="checkbox" name="balrdys" value="F,"id="chkA">Fresh Activation 
              <input type="text" name="balrdys" size="1" id="txtA"/><br>
          
              <input type="checkbox" name="balrdys" value=":A,">Auto-Renewal   
              <input type="text" name="balrdys" size="1"><br>
          
              <input type="checkbox" name="balrdys" value="NO">No<br>
          </td>
          login.js

          Code:
          if(document.getElementById('chkA').checked) {
                  var txt = document.getElementById('txtA');
                  if(txt.value.length == 0) {
                      window.alert("Enter Renewal Balance");
                      txt.focus();
                      return false;
                  }
              }
          Thanks,
          madhu
          Last edited by Dormilich; Jul 7 '10, 07:39 AM. Reason: making the code a bit more readable

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            what is not checking the checkboxes?

            Comment

            • madhuriks
              New Member
              • Jun 2010
              • 149

              #7
              Originally posted by Dormilich
              what is not checking the checkboxes?
              i need if any checkbox and textbox left blank before submitting the form..it should show an alert and if i leave checkbox and enter the textbox value..it is taking..but it should not take..it has to take both the checkbox and textbox value..if i left any thing..it should show an alert that 'Select checkbox'..

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                ok, I see the problem is the logic behind the validation.

                let’s design a programme:

                try to word your requirements in short sentences (there’s no limit how many you need). do not try to pack too much info in one. you do not even need to have the correct sequence.

                next, try to translate each sentence into code.

                if you have no idea how to break a sentence into code, break the sentence into smaller parts until you find a way to put it in code. if that’s not possible, you need to find another way to express the sentence.

                adjust the HTML code to your needs.

                Comment

                • madhuriks
                  New Member
                  • Jun 2010
                  • 149

                  #9
                  hi,
                  i got it...i had another doubt that the output should come like 'F,10:'..but i m getting as 'F,10'..i tried so many times..i m not getting can u suggest me where the code gets wrong...here i m sending jsp code,javascript code and servelet code..can u check it..i m waiting for the reply...

                  login.jsp

                  Code:
                  <td><font color="#800000" size=""><b>Low-Bal-RDays</b></font></td>
                                          <td><input type="checkbox" name="balrdys" value="F,"id="chkA">Fresh Activation <input type="text" name="balrdys" size="1" id="txtA"/><br>
                                              <input type="checkbox" name="balrdys" value="A,">Auto-Renewal   <input type="text" name="balrdys" size="1"><br>
                                              <input type="checkbox" name="balrdys" value="NO">No<br>
                                          <td></td>
                  login.js

                  Code:
                  if(document.getElementById('chkA').checked) {
                          var txt = document.getElementById('txtA');
                          if(txt.value.length == 0) {
                              window.alert("Enter Fresh Activation Days");
                              txt.focus();
                              return false;
                          }
                      }
                  login.java

                  Code:
                  // Getting Balance Retrieval Days
                          String[] balrdys = request.getParameterValues("balrdys");
                          String balrdysStr = "";
                          for (int i = 0; i < balrdys.length; i++) {
                              if (balrdysStr == null) {
                                  balrdysStr += balrdys[i];
                              } else {
                                  balrdysStr += balrdys[i];
                              }
                          }
                  Thanks and waiting for the reply,
                  madhu

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you have to ask in the Java forum, since I don’t know how Java processes form input.

                    Comment

                    • madhuriks
                      New Member
                      • Jun 2010
                      • 149

                      #11
                      Originally posted by Dormilich
                      you have to ask in the Java forum, since I don’t know how Java processes form input.
                      k..thanx..

                      Comment

                      • madhuriks
                        New Member
                        • Jun 2010
                        • 149

                        #12
                        Originally posted by Dormilich
                        you have to ask in the Java forum, since I don’t know how Java processes form input.
                        hi,
                        First of all I thank u that this site helps me a lot..in clarifying my doubts..in my jsp page..if i click one check box..remaining checkbox should be disabled...can u suggest me how to do it..

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          if you want only one of the boxes checked, use radio buttons with a common name.

                          Comment

                          • madhuriks
                            New Member
                            • Jun 2010
                            • 149

                            #14
                            Originally posted by Dormilich
                            if you want only one of the boxes checked, use radio buttons with a common name.
                            hi gud mrng,
                            i can use radio buttons but the problem is in my project somes times more than 2 check boxes should be selected..so i had used checkboxes instead of radio buttons can u tell me how to do if one check box is selected remaining should get disabled...

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              can u tell me how to do if one check box is selected remaining should get disabled...
                              use radio buttons in this particular case.

                              Comment

                              Working...