0
$('.btn-process-request', node).bind('click', function(e){
  e.preventDefault();
  var data = {};
  var fax_number_empty = {{ contract.request.customer.fax }}
  if (fax_number_empty == 0) {
    alert('Please, attach the fax number to your profile');
  }
  alert('Hello! What is your name?');
  if ($(this).data('reason')) {
      data.reason = prompt($(this).data('reason'));
      if (!data.reason.length && $(this).data('reason-required')) {
        alert($(this).data('reason-required'));
        return false;
      }
  }
  $.ajax({
    url : $(this).attr('href'),
    type: 'POST',
    data : data,
    success: function(data, textStatus, jqXHR) {
      if (data.success) {
          if (data.redirect_to) {
            window.location.href = data.redirect_to;
          }
          else if (data.reload) {
            window.location.reload();
          }
      }
      else {
        alert('Error! See console for details :(');
        console.error(textStatus, data);
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.error(textStatus, errorThrown);
    }
  });
  return false;
});

In this code, I've created the lines

var fax_number_empty = {{ contract.request.customer.fax }}
      if (fax_number_empty == 0) {
        alert('Please, attach the fax number to your profile');
      }

I don't how to implement this correctly. I'd like say, ok, if contract.request.customer.fax is empty, then display an alert which will say 'Please, attach the fax number to your profile.' The most important thing here is to stop executing. In other words, what is bellow those lines would not be executed. Could anyone have time to tell me how to improve that code?

P.S. Please don't be shy to let me know if my question is unclear.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • Possible duplicate of [Is there a standard function to check for null, undefined, or blank variables in JavaScript?](http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in) – rolspace May 19 '17 at 13:27
  • What's wrong with just adding an `else { ... }` so that what is in the `else` will only be executed if `fax_number_empty != 0`. – Matt Cremeens May 19 '17 at 13:27
  • try ` fax_number_empty == null ` this checks if it's null and if `typeof(fax_number_empty) === "undefined"` – Cr1xus May 19 '17 at 13:30
  • What do you mean? Put the bottom line between `{}` with the else? – David Hilbert May 19 '17 at 13:30

1 Answers1

1

Just return in case the field fax_number_empty is a falsy value , and the function will jump to the end avoiding the rest of the code.

   if (!fax_number_empty) {
     alert('Please, attach the fax number to your profile');
     return; 
   }
Community
  • 1
  • 1
Karim
  • 8,454
  • 3
  • 25
  • 33