Get value other than null javascript

Asked

Viewed 1,534 times

1

I have a function in Javascript, and anyway I put it enters the function, I want it not enter if the value has null, because it is giving error. It only needs to enter the function if attachment is different from null. Go on like 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;
                    }
                })
            }
        });

Anyway I put, it always returns me the error in this line:

$('#<%=Annex.Clientid %>'). change(Function () {

If the Attachment ta filled does not return error, but it may happen that it is empty. And it cannot return error, so I am trying to use if.

Editing: After many attempts, I arrived at the following error:

Undefined object reference for an object instance. Which occurs on this line: $('#<%=Attachment.Clientid %>'). change(Function () {

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

Editing: I tried putting 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 of:

Undefined object reference for an object instance.

  • In your condition, try to do: if (document.getElementById("Anexo") && document.getElementById("Anexo").value) { ... }

  • The same problem happens, the error keeps happening on the same line.

  • So please edit the question and describe your problem better, preferably by creating a [mcve], as it is difficult to understand what is happening.

  • Even Attachment being null, it enters this function, in case it was null could not enter, and anyway q I put != null != Undefined != "" it always enters the function.

  • As edited @Lucascosta the problem is not only in checking the empty component, so when I declare the component, the error occurs in it. So much so that I have tried various forms, and not the right one, including the example.

  • @marianac_costa The way you see the question originally, saying that the error was in Javascript, generated answers that would solve the reported problem, as well as be duplicate of the question that Lucas cited. I advise you to create another question focusing the problem on C#.

  • But the error occurs in the javascript function, put the if to check if it was empty, was a solution that I thought could work, more unfortunately all the answers I was given, none solved my problem.

  • Tried to if ( document.getElementById("Anexo").value ) { @marianac_costa? If he comes in anyway, put a console.log( document.getElementById("Anexo").value ) before $('#<%=Anexo.ClientID %>').change to see if the attachment is empty or not.

  • I did the test, thus @Lucascosta, the value that appeared was: Undefined , continues giving the same error in the Annex line.Clienteid.

  • If you are entering when Undefined has some confusion wrong there because it will test all the conditions that is in the answer I have indicated as duplicate. Test with if ( document.getElementById("Anexo").value != 'undefined ' )

  • @Lucascosta I did the test as you reported, look how I put: var person = { name: Document.getElementById("Attachment"). value } if (!person.name){ I don’t know if I did it right. But it keeps making mistakes in the same place!

  • It’s wrong. The other person’s answer is just an example, and you’re denying the condition. Try exactly if ( document.getElementById("Anexo").value ) { in place of your if. If you enter anyway try if ( document.getElementById("Anexo").value != 'undefined' ) {. If you log in anyway, create a vericavél example for us @marianac_costa, please.

  • The error is in . NET, not Javascript. It’s the object Anexo do . NET that does not exist. No solution solved the problem because you asked for something from Javascript, but the problem is not in it. Understand this.

  • Yes @Andersoncarloswoss after much study, I discovered the problem, but really the java part, helped me a lot of IF.

Show 10 more comments

2 answers

5


Considering this condition:

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

The condition will be true when:

  1. obj.value is different from ""; or
  2. obj.value is different from null; or
  3. obj.value is different from undefined;

So if the value is a string empty, condition 2 is met 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 met and the code is executed:

const obj = {"value": null};

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

Already, if the value is undefined, the condition 1 is also met 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 that none of the above options should make the code run, so do:

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

For thus, if the object does not exist, the first operand of the expression will return false. Already, if the attribute value of the object not existing or being null, the second operand will return false. See below 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 in 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 is executed.

  • I’ll do the tests, thank you.

  • Good morning Anderson, however I put it, still giving error in the same line: $('#<%=Attachment.Clientid %>'). change(Function () { If it was empty it should not enter, do not know why it enter and gives the error on this line. The error it gives is: Undefined object reference to an instance of an object.

  • Ah, so your problem isn’t with Javascript. What language is this that you’re using? Where does this value come from Anexo? Why this error message was not in the question from the beginning?

  • Anderson, after many changes I got in this error, I still have not edited the question because I could only check now. Attachment, is a fileUpload, I use in C# Asp.Net.

  • Then put your C# code in the question and the complete error message.

  • Behind the Attachment (fileupload) has no code, the error gives in this part of Javascript, already edited the question. Thanks.

  • In which part of the code you define the variable Anexo?

  • Attachment is the ID of the Fileupload component. So I don’t declare it, I use the component value, you think if I declared to take the value, it would be more viable ?

  • Is it a component in C# or HTML/JS? What would this be ClientId?

  • is a component of C#, Clientid provides the value for the HTML attribute " id " of the HTML tag of this server control.

Show 5 more comments

2

Lacked some ; in your code. You also have a problem with your verification, if the value were null the verification document.getElementById("Anexo").value != "" would return true, and how you were using the ||(or), if any of the checks returned true the answer would be true. Then replaces the ||(or) by &&(e). That is, the answer 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;
                }
            });
    }
});
  • This does not seem to solve the problem. If the value is null, the first condition, value != "", will be satisfied, then enters the if.

  • Still with the same problem, the error always occurs in this line " $('#<%=Attachment.Clientid %>'). change(Function() {", if does not seem to work, I put in the google console, to check the value when it is empty, and it appears like this: Document.getElementById("Attachment"). value ""

  • Well noted @Andersoncarloswoss, I will change the answer

  • @Robertofagundes falls into the same error, and does not work the rest of the function, even when I insert a file within the pattern;

    1. should use && and not || or. 2) after the condition does not make a function waiting for a new user action. 3) Retrieve dynamic information in variables so you can check if it has content. var foo = '<%=Attachment.Clientid %>';
  • @Wilsonrosagomes I did so, I switched to && and continued the same problem, he informs me this error Object reference not defined for an instance of an object, in this line: $('#<%=Attachment.Clientid %>'). change(Function() {.In the debug of Google Chrome, it returns me the value "", ie empty.

  • @marianac_costa would have to post your html code, you are using classic Asp <%=Attachment.Clientid%>, if yes, post the.asp file as well. Because as I mentioned should not use a function that expects action from the user within an IF condition.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.