10 Replies Latest reply: Dec 6, 2012 12:08 AM by Prabodh RSS

    Validate Apex_Item using Javascript

    Sudhir_Meru
      Hi,

      Below query has two text box, When the two text box is null and user clicks on the button it must alter the user saying enter value.

      SELECT
      apex_item.text(1, NULL, 4, 4, NULL, 'f01_#ROWNUM#') ONE,
      apex_item.text(2, NULL, 4, 4, NULL , 'f02_#ROWNUM#') TWO
      FROM DUAL


      I tried the below code using java script but this is not working,
      <script type="text/javascript">
      function port(pRow) {
      var txb = $x_Item('f01_'+pRow); // I don't know which syntax is this!
      if ( txb.value == '' ) {
      alert('Enter value in first box');
      txb.style.backgroundColor = '#a00';
      }
      else{
      txb.style.backgroundColor = ''; //return to default color
      }
      }
      </script>
      The above script i wrote only for first text box, Please suggest me to modify the code.

      Thanks
      Sudhir
        • 1. Re: Validate Apex_Item using Javascript
          Prabodh
          Apex version?
          If on 4.x use DA, it will be much simpler.
          If older, where are you calling the JS function port(pRow) ? Which event? Post that code ,too.
          Cheers,
          • 2. Re: Validate Apex_Item using Javascript
            Sudhir_Meru
            I am using Apex 3.2, This is a manual tabular form. I have posted both the code in first post sql and javascript.


            I am calling the function on the button click


            Please suggest me if there is any alternative method if available.


            Thanks
            Sudhir
            • 3. Re: Validate Apex_Item using Javascript
              Prabodh
              I am using Apex 3.2, This is a manual tabular form. I have posted both the code in first post sql and javascript.
              I am calling the function on the button click

              Please suggest me if there is any alternative method if available. >

              Tabular form means multiple rows. The alert should come only once for the page or for each occurrence of null value?

              Cheers,
              • 4. Re: Validate Apex_Item using Javascript
                Prabodh
                Try this
                <script type="text/javascript">
                function port() {
                  var txb = document.getElementsByName('f01');
                  var null_found boolean = false;
                  for (i=0; i< txb.length;i++) {
                      if ($v(txb.id)) {
                txb[i].style.backgroundColor = ''; //return to default color
                } else {
                txb[i].style.backgroundColor = '#a00';
                null_found = true;
                }
                }
                if (null_found ) {
                alert('Enter value in first box');
                }
                }
                </script>
                Cheers,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: Validate Apex_Item using Javascript
                  VC
                  Sudhir_Meru wrote:
                  I am using Apex 3.2, This is a manual tabular form. I have posted both the code in first post sql and javascript.


                  I am calling the function on the button click


                  Please suggest me if there is any alternative method if available.


                  Thanks
                  Sudhir
                  You have to loop through and highlight the row that was not filled

                  If you are using jQuery try
                  $('.notnull').remove();
                  //replace f01 with your column names
                  $('input[name="f01"]').each(function (index) {
                       if (this.value == '') {
                            $(this).parent().append('<div class="notnull" style="color:red;">This must have some value</div>');
                       }
                  });
                  Edited by: VC on Dec 5, 2012 1:11 PM
                  • 6. Re: Validate Apex_Item using Javascript
                    Prabodh
                    AFAIK, Apex 3.2 did not have jQuery built-in. You had to include it if you wanted to use it.
                    Anyway, I do not have a 3.2 instance anymore. This is more out of memory.

                    Cheers.
                    • 8. Re: Validate Apex_Item using Javascript
                      Prabodh
                      It is a snippet that I wrote for your query.
                      If you cannot spot the differences then its your issue.
                      • 9. Re: Validate Apex_Item using Javascript
                        Sudhir_Meru
                        Got the soloution below. I Fixed Got the solution. Spot the difference It was my mistake

                        <script>
                        function nullcheck()
                        {
                        var v = document.getElementById('f01');
                        if(document.getElementById('f01').value=="")
                        {
                        alert("Please enter value.");
                        v.style.backgroundColor = "red";
                        return false;
                        }
                        else
                        {
                        v.style.backgroundColor = "white";
                        }
                        }
                        </script>

                        Thanks For Suggestion
                        • 10. Re: Validate Apex_Item using Javascript
                          Prabodh
                          First you say it is a tabular forms. And now you say it works with
                          var v = document.getElementById('f01');
                          Strange. The above should not return any elements because there should be no id=f01 in tabular form.

                          Cheers,