You are viewing limited content. For full access, please sign in.

Question

Question

Create an alert in Table for individual fields in a Row

asked on October 26, 2017

Hi,

 

I am trying to alert and disable the submit button when a field changes it's value to less than 0.

I have read man answers on this and not able to figure it out.  Most of the java script are working only on the first line or when you change the last field to > 0 the condition is being missed.

I came up with the following code which seem to alert many times on a condition and the submit is not being hide.

$(document).ready(function () {
   var $submit = $('.Submit');
      $('li.cf-table-block').change(rowtotal);
    function rowtotal() {
             var adjqty = 0;
        $('td.adjqty').each(function () {
            var s = 0;
            $(this).find('input').each(function () {
                s += parseNumber($(this).val());
            });
          if (s < '0'){
         alert(s);
         $submit.hide();
      }
      else{
         $submit.show();
      }
          adjqty += s;        
        });
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

Can someone guide me on this please?

 

Thanks!

AJ

0 0

Replies

replied on October 27, 2017 Show version history

I think we can find a simpler way to do this for you.  Here's two options:

  1. If you are using a number field in your table, you can set minimums and maximums on the Layout Page, you can set the minimum to zero, which would just prevent the entry of negative values.
     
  2. The other way I would do it is as follows:
  • Add a Single Line field to your layout, give it a CSS Class Name of "result_field".  Also give it a Calculation of: =IF(MIN(Table_Variable.Number_Field_Variable)<0,"NEGATIVE", "POSITIVE")     Where Table_Variable.Number_Field_Variable is the variable of the field within your table (I recommend using the variable picker in the calculation field to ensure there are no typos.  This formula will populate this field with either NEGATIVE or POSITIVE based on the lowest value in the fields in the table.
  • Add this CSS to your form (which will hide the result_field):
    .result_field {display: none;} 
  • Add this Javascript to your form (which will run each time the result_field changes between POSITIVE or NEGATIVE - not each time the table values change, just when it changes between POSITIVE and NEGATIVE): 

    $(document).ready(function () {
      
      $('.result_field input').change(function(){ 
        if ($('.result_field input').val() == 'POSITIVE') { 
          $('.Submit').show();
        }
        else if ($('.result_field input').val() == 'NEGATIVE') { 
          alert('Warning! There is a negative value.');
          $('.Submit').hide();
        }
      });
      
    });
  • It'll start out with the hidden field as POSITIVE (minimum of zero) and allow the Submit button to show.  When a negative value is entered, the alert will pop-up and the Submit button will be hidden.  The button will only be shown again when the negative values are removed.

 

0 0
replied on October 30, 2017

Thanks for the reply.

 

option 1 doesn't work on ready-only fields in a table.

option 2 again is not working at all.

 

Thanks!

0 0
replied on October 30, 2017

With option 2 - if you remove the result_field CSS Class from the field, it should be shown, and then you should be able to see its value changing from POSITIVE to NEGATIVE. 

Are you seeing that, or a different result?

0 0
replied on October 30, 2017

value is being changed to POSITIVE to NEGATIVE. but the submit button is not being disabled.

0 0
replied on October 31, 2017

That's good, so we know the formula is working.

How about the alert pop-up - is that showing?  If not, that could mean an issue with the Javascript.

If not, maybe try this...

Here is the same Javascript code but with some alerts throughout to verify which parts are working vs. not working.  You could try this and then confirm which alert pop-ups you see.

$(document).ready(function () {
  
  alert('one');
  $('.result_field input').change(function(){ 
    alert('two');
    if ($('.result_field input').val() == 'POSITIVE') { 
      alert('three');
	  $('.Submit').show();
    }
    else if ($('.result_field input').val() == 'NEGATIVE') { 
	  alert('Warning! There is a negative value.');
      $('.Submit').hide();
    }
  });
  
});

 

0 0
replied on November 2, 2017

Hi Matthew,

 

Thanks for your help.

 

I tried the following and got it working.  

 

$(document).on('change', '.ValidateQtyField input', hideSubmitButton);
$(document).ready(hideSubmitButton);     
function hideSubmitButton() {
var i = 0;
 $(".ValidateQtyField input").each(function(){
   var $submit = $('.Submit'); 
     if ($(this).val() < '0') {
             i++;}
          else {
      }
if (i > 0) {
     $submit.hide();
}
else
{
  
     $submit.show();
}
   });
 }

 

Thanks!
 

AJ

1 0
replied on November 2, 2017

Glad to help, and glad you got it working.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.