in MVC Edit page, how to check for null values?
-
$(window).load(function () { if ($('#TTest1').val(null)) { $('#TTest1').val("No"); }
Somewhat new to mixing in javascript or jquery into html, The goal is to test the values being passed into the form, and on some of those, if they are null? make the value = "No". The result above is hit or miss, it sometimes works and usually does not. I can see the value coming from the sql management studio is null, i can put an alert within the test in my IF statement, and it never fires the message. but another set of variables using the exact same syntax works fairly well. My question is how to manipulate the variable so that it tests it reliably and can set the value to string "No" if null? I wondered if its doing a double-reference and because of this producing invalid results? i mean the statement tests the variable then writes it? the variables are passed in from the c# view, and the edit form can change then submit and they get updated in the database. Well there seems to be some challenge in looking at a variable and testing it to be null. javascript is ok to use it just has to be reliable and work every time.
-
$(window).load(function () { if ($('#TTest1').val(null)) { $('#TTest1').val("No"); }
Somewhat new to mixing in javascript or jquery into html, The goal is to test the values being passed into the form, and on some of those, if they are null? make the value = "No". The result above is hit or miss, it sometimes works and usually does not. I can see the value coming from the sql management studio is null, i can put an alert within the test in my IF statement, and it never fires the message. but another set of variables using the exact same syntax works fairly well. My question is how to manipulate the variable so that it tests it reliably and can set the value to string "No" if null? I wondered if its doing a double-reference and because of this producing invalid results? i mean the statement tests the variable then writes it? the variables are passed in from the c# view, and the edit form can change then submit and they get updated in the database. Well there seems to be some challenge in looking at a variable and testing it to be null. javascript is ok to use it just has to be reliable and work every time.
Member 11733868 wrote:
if ($('#TTest1').val(null))
The
.val(value)
version[^] of theval
method sets the value of the matched elements to the specified value, and returns the set of matched elements. Due to javascript's strange concept of "truthy" variables, the returned jQuery object will always be considered to be "true", and the code within theif
block will always execute. If you want to test whether the specified element's value is empty, you need to callval
without passing any arguments, and then test the return value:if ($('#TTest1').val() === '') { ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Member 11733868 wrote:
if ($('#TTest1').val(null))
The
.val(value)
version[^] of theval
method sets the value of the matched elements to the specified value, and returns the set of matched elements. Due to javascript's strange concept of "truthy" variables, the returned jQuery object will always be considered to be "true", and the code within theif
block will always execute. If you want to test whether the specified element's value is empty, you need to callval
without passing any arguments, and then test the return value:if ($('#TTest1').val() === '') { ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
ok thanks this is helpful, but its 3 equals signs? it makes sense variablename.val() is equal to ...but 3 === i forgot about the truthiness factor. let me ask you from another angle, is the javascript and jquery able to run code thats mission critical? in other words is the execution predictable that it will run exactly as coded every time? i want to understand, it seems like with browser/html it could run until interrupted by a mouse movement or ? and it could lose its place?
-
$(window).load(function () { if ($('#TTest1').val(null)) { $('#TTest1').val("No"); }
Somewhat new to mixing in javascript or jquery into html, The goal is to test the values being passed into the form, and on some of those, if they are null? make the value = "No". The result above is hit or miss, it sometimes works and usually does not. I can see the value coming from the sql management studio is null, i can put an alert within the test in my IF statement, and it never fires the message. but another set of variables using the exact same syntax works fairly well. My question is how to manipulate the variable so that it tests it reliably and can set the value to string "No" if null? I wondered if its doing a double-reference and because of this producing invalid results? i mean the statement tests the variable then writes it? the variables are passed in from the c# view, and the edit form can change then submit and they get updated in the database. Well there seems to be some challenge in looking at a variable and testing it to be null. javascript is ok to use it just has to be reliable and work every time.
Is that how you do it in any other language? Clearly you have less idea of how relational operators work.
$('el').val(null);
Code would set the value of that (input only) element to null. It doesn't perform any comparison at all. Which is done by using equality check. You need to use,
if($('#TTest1').val() == '')) { // No type checked
$('#TTest1').val('No');
}This would now work. Another way has been shared by Richard, that is an equality check that also check whether their type is same or not, for example '1' == 1 is true (where as in C# it would be false, because character 1 is not equal to integer 1). For such scenarios you use === which checks the type also. In such case, '1' === 1 would return false. Which means they might have same string representation but not same type. Use that code in order to determine their type and value.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
ok thanks this is helpful, but its 3 equals signs? it makes sense variablename.val() is equal to ...but 3 === i forgot about the truthiness factor. let me ask you from another angle, is the javascript and jquery able to run code thats mission critical? in other words is the execution predictable that it will run exactly as coded every time? i want to understand, it seems like with browser/html it could run until interrupted by a mouse movement or ? and it could lose its place?
Quote:
is the execution predictable that it will run exactly as coded every time?
100% sure it would run the same every time, unless your JavaScript engine tinkers with something else. In other words, it is a standard language and
alert(message)
always pops out a dialog box instead of closing the window (which is done by usingwindow.close()
). I have never seen such thing ever happening.The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
ok thanks this is helpful, but its 3 equals signs? it makes sense variablename.val() is equal to ...but 3 === i forgot about the truthiness factor. let me ask you from another angle, is the javascript and jquery able to run code thats mission critical? in other words is the execution predictable that it will run exactly as coded every time? i want to understand, it seems like with browser/html it could run until interrupted by a mouse movement or ? and it could lose its place?
Regarding the three equals signs, this is the "strict" equality operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators[^] In javascript, the regular equality operator (
==
) will convert the operands if they are not of the same type, which can lead to confusing and incorrect results.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Is that how you do it in any other language? Clearly you have less idea of how relational operators work.
$('el').val(null);
Code would set the value of that (input only) element to null. It doesn't perform any comparison at all. Which is done by using equality check. You need to use,
if($('#TTest1').val() == '')) { // No type checked
$('#TTest1').val('No');
}This would now work. Another way has been shared by Richard, that is an equality check that also check whether their type is same or not, for example '1' == 1 is true (where as in C# it would be false, because character 1 is not equal to integer 1). For such scenarios you use === which checks the type also. In such case, '1' === 1 would return false. Which means they might have same string representation but not same type. Use that code in order to determine their type and value.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
were you saying that comparison to ="" is not valid? its sometimes a reference to null, right? is there any way you know of that could make visual studio debug this script? if i put breakpoints on the script it says no, not supported. fiddle is nice to check syntax; three equals's checks types, so its a bit clearer now
-
Quote:
is the execution predictable that it will run exactly as coded every time?
100% sure it would run the same every time, unless your JavaScript engine tinkers with something else. In other words, it is a standard language and
alert(message)
always pops out a dialog box instead of closing the window (which is done by usingwindow.close()
). I have never seen such thing ever happening.The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
right, when i have code that works sometimes but not every time, i wonder what the engine is doing. So, would you say that an application like controlling equipment would not be advisable with html/javascript? where it reads a value of something and takes action like sets a voltage that runs a motor, etc? its okay if its like that, am just trying to be clear what its doing, how 'stateful' is it? i mean there are websites like amazon where you look at items and it may mess up but you just reload and go. if the html/javascript was running motors that moved mechanisms is it stateful and trustable to keep its place?
-
Regarding the three equals signs, this is the "strict" equality operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators[^] In javascript, the regular equality operator (
==
) will convert the operands if they are not of the same type, which can lead to confusing and incorrect results.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
ok thanks for that. === is strict equality, nulls compare when they are null and undefined, == converts the types but hopefully converts a copy; so if it converts and you reference something more than once that got converted, it can be a problem is that what you mean? I assumed strict equality, but will use this in the future. it seems that the javascript/html cannot be trusted for lambda type expressions like c#, just do everything a step at a time i had done switch case, case yes, no, null, "" then default with the alert message "it should never get here". of course you can guess the message that shows up. value was no. or other code tested for null and would be true when the value was string 'no', and othertimes not. but i think the answer is to separate the tests and not try to combine and hope the test will be run right.
-
were you saying that comparison to ="" is not valid? its sometimes a reference to null, right? is there any way you know of that could make visual studio debug this script? if i put breakpoints on the script it says no, not supported. fiddle is nice to check syntax; three equals's checks types, so its a bit clearer now
Debug your JavaScript code inside your browser. Browsers also support this feature.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
right, when i have code that works sometimes but not every time, i wonder what the engine is doing. So, would you say that an application like controlling equipment would not be advisable with html/javascript? where it reads a value of something and takes action like sets a voltage that runs a motor, etc? its okay if its like that, am just trying to be clear what its doing, how 'stateful' is it? i mean there are websites like amazon where you look at items and it may mess up but you just reload and go. if the html/javascript was running motors that moved mechanisms is it stateful and trustable to keep its place?
Yes they are totally trust-worthy and you can put your faith in it. It is just the browser that keeps tinkering, and the performance and other stuff depends on browser itself. JavaScript would never do something that is not written, and would always do what is written.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Debug your JavaScript code inside your browser. Browsers also support this feature.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
debug as in single step? is there no add in to visual studio?