Get value other than null javascript

1

I have a function in Javascript, and every time I put it in the function, I want it not to enter if the value has null, because it is giving error. It only needs to get in the function if attachment is other than null. Here's how I'm doing:

   $(function () {
            if (document.getElementById("Anexo").value != "" || document.getElementById("Anexo").value != undefined || document.getElementById("Anexo").value != null) {
                $('#<%=Anexo.ClientID %>').change(function () {
                    var f = this.files[0]

                    if (f.size > 8388608 || f.fileSize > 8388608) {
                        alert("Tamanho excede o limite permitido.")

                        this.value = null;
                    }
                })
            }
        });

In any way I put it, it always returns the error in this line:

  

$ ('# '). change (function () {

If the Annex is filled it does not return an error, but it may happen to be empty. And it can not return error, so I'm trying to use the if.

Editing: After many attempts, I came up with the following error:

  

Object reference not set to an instance of an object.   What happens in this line:    $ ('# <% = Attachment.ClientID% >'). change (function () {

I use the Asp.net C # language. Attachment is a FileUpload component, this error occurs only when the component is empty, so it would need to check if it is null to perform the function.

Editing: I've tried putting it in a variable, like this:

var label = document.getElementById("<%=Anexo.ClientID%>");

When I declare in the variable, then it gives me the error in this line, and the same error as:

  

Object reference not set to an instance of an object.

    
asked by anonymous 20.07.2017 / 17:15

2 answers

4

Considering this condition:

if (obj.value != "" || obj.value != null || obj.value != undefined) {
  console.log("Condição foi satisfeita");
}

The condition will be true when:

  • obj.value is different from "" ; or
  • obj.value is different from null ; or
  • obj.value is different from undefined ;
  • Then, if the value is an empty string , condition 2 is satisfied and the code is executed:

    const obj = {"value": ""};
    
    if (obj.value != "" || obj.value != null || obj.value != undefined) {
      console.log("Condição foi satisfeita");
    }

    If the value is null, condition 1 is satisfied and the code is executed:

    const obj = {"value": null};
    
    if (obj.value != "" || obj.value != null || obj.value != undefined) {
      console.log("Condição foi satisfeita");
    }

    If the value is undefined, condition 1 is also satisfied and the code is executed:

    const obj = {"value": undefined};
    
    if (obj.value != "" || obj.value != null || obj.value != undefined) {
      console.log("Condição foi satisfeita");
    }

    And finally, if the value does not exist an error will be triggered.

    const obj = undefined;
    
    if (obj.value != "" || obj.value != null || obj.value != undefined) {
      console.log("Condição foi satisfeita");
    }

    I believe none of the above should make the code execute, so do:

    if (obj && obj.value) {
      console.log("Condição foi satisfeita");
    }
    

    Well, if the object does not exist, the first operand of the expression will return false. However, if the value attribute of the object does not exist or is null, the second operand will return false. See below for all previous tests on this condition:

    const tests = [
      {"value": ""},
      {"value": null},
      {"value": undefined},
      undefined
    ];
    
    for (obj of tests) {
      if (obj && obj.value) {
        console.log("Condição foi satisfeita");
      }
    }

    Note that the code does not run on all tests. However, if you set a value for the attribute:

    const obj = {"value": "foo"};
    
    if (obj && obj.value) {
      console.log("Condição foi satisfeita");
    }

    The code runs.

        
    20.07.2017 / 20:20
    2

    Some ; was missing from your code. It also has a problem checking it, if the value was null the document.getElementById("Anexo").value != "" check would return true , and since you were using || (or), if any of the checks returned true a response would be true . Then replace || (or) with && (e). That is, the response will be true only if all checks return true :

    $(function () {
        if (document.getElementById("Anexo").value != "" && 
            document.getElementById("Anexo").value != undefined && 
            document.getElementById("Anexo").value != null) {
                $('#<%=Anexo.ClientID %>').change(function () {
                    var f = this.files[0];
                    if (f.size > 8388608 || f.fileSize > 8388608) {
                        alert("Tamanho excede o limite permitido.");
                        this.value = null;
                    }
                });
        }
    });
    
        
    20.07.2017 / 17:17