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) { ... }
– Woss
The same problem happens, the error keeps happening on the same line.
– Mariana
So please edit the question and describe your problem better, preferably by creating a [mcve], as it is difficult to understand what is happening.
– Woss
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.
– Mariana
Possible duplicate of How to check if a String is empty in Javascript?
– BrTkCa
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.
– Mariana
@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#.
– Woss
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.
– Mariana
Tried to
if ( document.getElementById("Anexo").value ) {
@marianac_costa? If he comes in anyway, put aconsole.log( document.getElementById("Anexo").value )
before$('#<%=Anexo.ClientID %>').change
to see if the attachment is empty or not.– BrTkCa
I did the test, thus @Lucascosta, the value that appeared was: Undefined , continues giving the same error in the Annex line.Clienteid.
– Mariana
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 ' )
– BrTkCa
@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!
– Mariana
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 tryif ( document.getElementById("Anexo").value != 'undefined' ) {
. If you log in anyway, create a vericavél example for us @marianac_costa, please.– BrTkCa
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.– Woss
Yes @Andersoncarloswoss after much study, I discovered the problem, but really the java part, helped me a lot of IF.
– Mariana