if condition cannot catch the null value of my front end username field in javascript and html - html

I have a username text field in the login.html file.
I have javascript code as follows
loginbtn.addEventListener("click", function() {
if(username.value != null) {
socket.emit("login", {username:username.value});
$("#name").hide();
$("#mario-chat").show();
} else {
err.innerHTML = document.write("please enter username");
}
});
In the login page i have not entered any name but still my code goes to the if condition and executing the 1$("#name").hide();$("#mario-chat").show();.
Please tell me why my if condition is executing rather than else part. thanks in advance.

If you console.log(username.value) the result will probably not be null or undefined. That is why != null does not the trick.
username.value might be an empty string so you can add && username.value != '' to the if statement. if(username.value != null && username.value != ''){}

Related

Google Appscript IF Or statement not working

Good day everyone; I am running into an error I can't explain. The scenario is as follows, I have two input boxes that collect information. If no value is entered, I want the if statement to handle it and cause a break. The Input box also has an "x" to close the box, which returns a value of "Cancel". What I am trying to do is capture a condition where if no value is entered OR cancel is passed through, a break will occur. Right now, the problem is Google completely ignores the Or statement. I know individually, my IF logic works, but when coupled with OR it doesn't recognize the condition.
This is my current code:
var propnumber = Browser.inputBox('Enter RFI/RFQ Number', Browser.Buttons.OK);
if(propnumber != "" || propnumber != 'cancel'){} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
var myName = Browser.inputBox("Enter the Component Name",Browser.Buttons.OK_CANCEL);
if(myName != 'cancel')
{
I do something
}
As I mentioned in my description, my propnumber condition ignores the or and always accepts the value of cancel or blank. If I remove the or ( || ) then it works with one condition at a time.
I am sure this is something trivial any help appreciated.
What's wrong
The logic in the following part of your code
if(propnumber != "" || propnumber != 'cancel'){
// I assume some code will go here
} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
does not match the logic you've described here:
if no value is entered OR cancel is passed through, a break will occur.
Consider the case where propnumber is 'cancel':
propnumber != "" evaluates to true
propnumber != 'cancel' evaluates to false
Therefore the if(... || ...) condition in your code evaluates to true and the (currently empty) if block runs, rather than the else.
How to fix it
Option 1: A literal translation of the logic
if no value is entered OR cancel is passed through, a break will occur
would be
if(propnumber == "" || propnumber == 'cancel') {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
} else {
// Some action
}
Option 2: If you wish to swap the if and else clauses, you must negate the entire condition. So this will also work:
if(!(propnumber == "" || propnumber == 'cancel')) {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}
Note the added parentheses and single negation.
Option 3: use AND instead of OR in your existing code.
The expression !(A || B) is NOT logically equivalent to !A || !B. Instead, it is equivalent to !A && !B (see DeMorgan's Law). So this will also work:
if(propnumber != "" && propnumber != 'cancel') {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}

How to compare numeric values in the function node in node-red?

I've been trying to do a simple numeric comparison since days in my function node but I really don't have any idea why it's not working. I have a function node which accepts two values. I've even converted it from object to Number but still the comparison won't work. Please find the full flow here:
[{"id":"39421a3d.5cda36","type":"function","z":"251d0ac6.958a36","name":"getL1MagneticCount","func":"msg.payload = {\"getCarCount1\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":606.6666259765625,"wires":[["31136d74.228fb2"]]},{"id":"a171070a.1ba198","type":"function","z":"251d0ac6.958a36","name":"getL2MagneticCount","func":"msg.payload = {\"getCarCount2\":msg.payload.Car};\nreturn msg;","outputs":1,"noerr":0,"x":586.6666259765625,"y":719.9999732971191,"wires":[["31136d74.228fb2"]]},{"id":"31136d74.228fb2","type":"function","z":"251d0ac6.958a36","name":"comparison","func":"var count1 = Number(msg.payload.getCarCount1);\nvar count2 = Number(msg.payload.getCarCount2);\n\nif(count1 >= count2){\n console.log(\"In\");\n msg.payload = msg.payload.getCarCount1;\n return [msg,null];\n \n} else {\n console.log(\"Out\");\n msg.payload = msg.payload.getCarCount2;\n return [null,msg];\n \n}","outputs":"2","noerr":0,"x":824.4443950653076,"y":663.3333148956299,"wires":[["57c8e7b7.c948e8"],["10b4a39f.16338c"]]},{"id":"57c8e7b7.c948e8","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"payload","x":1025.5556182861328,"y":626.6666140556335,"wires":[]},{"id":"10b4a39f.16338c","type":"debug","z":"251d0ac6.958a36","name":"","active":true,"console":"false","complete":"false","x":1028.8889236450195,"y":709.9999084472656,"wires":[]},{"id":"1a6938ca.0d2bf7","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"x":256.6666679382324,"y":605.555606842041,"wires":[["39421a3d.5cda36"]]},{"id":"d23e60e5.adb83","type":"inject","z":"251d0ac6.958a36","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"x":254.66665649414062,"y":719.5555419921875,"wires":[["a171070a.1ba198"]]}]
Please tell me where my mistake is. Thank you very much.
The problem is that each of the input messages are handled as independent events in the function node, so you only ever have 1 value to compare each time a message arrives.
What you need to do is make use of the context to store values between each message. Something like this:
//get stored values if present
var count1 = context.get("count1");
var count2 = context.get("count2");
if (msg.payload.hasOwnProperty("getCarCount1")) {
count1 = msg.payload.getCarCount1;
context.set("count1", count1);
}
if (msg.payload.hasOwnProperty("getCarCount2")) {
count2 = msg.payload.getCarCount2;
context.set("count2", count2);
}
if (count1 != undefined && count2 != undefined) {
if(count1 >= count2){
console.log("In");
msg.payload = count1;
return [msg,null];
} else {
console.log("Out");
msg.payload = count2;
return [null,msg];
}
}
I'm saving to global variables for the beginning temperature and humidity data from different sensors. In a block I'm checking these variables for "is not null", "nan" and then condition.. Maybe it will help.

Extra "Space" at end of "If" Requirement AS3

My code works, except it is requiring and extra "Space" at the end to be placed in order for the button to activate? Any ideas? I obviously don't have the space at the end of the user names or passwords in the code. This happens on another frame as well where I have the user type in a web address, I have the conditional set as == "md.website.com" but it is requiring "md.website.com " (extra space at the end) in order for the button to activate.
This code is expecting "AB1234 " and "newuser " instead of "AB1234" "newuser" like I need and I am telling it... I'm sorry, I'm new to AS3 and learning ALL I can, this site rocks for all the help I've already gotten!
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text == "AB1234" && password_txt.text == "newuser" )
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
The problem is that the TextEvent.TEXT_INPUT fires before the text field is actually updated. Try using Event.CHANGE instead (or using two TextEvent.TEXT_INPUT callbacks and appending the input character with event.text within each).
I don't know why AS3 is requiring the extra space, but I removed the exact conditional, and just did a minimum character count. Of course the trainee can then type in anything as long as it matches the minimum requirement, but again, the actual usernames and passwords don't matter, its all simulation anyways, here is the code with the character count....
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text != "" && username_txt.length >=5 &&
password_txt.text != "" && password_txt.length >=6)
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}

Check if localStorage isset or not

I want to check if localStorage is isset or not.
e.g. i am creating sample localstrorage:
localStorage.setItem('TODO_LIST', 'value');
Now, i want to check on page load whether localStorage with name TODO_LIST is isset or not, also if it is isset how to check whether it blank or not.
How about:
If (localStorage.getItem('TODO_LIST') && localStorage.getItem('TODO_LIST') !== '') {
// Do something when it's set and not empty
}
localStorage.getItem('TODO_LIST') returns null or falsy when it's not set.
localStorage.getItem('TODO_LIST') !== '' returns true if it's not empty.

if-check in HTML gone awry

I wrote a short function to do some error checks for a form and am stuck at a portion of code where the final 'elseif' clause in the code below keeps getting executed, even when there is text in the textbox...
could you please advise...thank you..
function errorCheck(){
if(!isInteger(document.getElementById("appleQty").value)){
alert('Please key in an integer in the Apple Quantity text box.');
document.getElementById("appleQty").value="";
document.getElementById("appleQty").focus();
return false;
}
else if(!isInteger(document.getElementById("orangeQty").value)){
alert('Please key in an integer in the Orange Quantity text box.');
document.getElementById("orangeQty").value="";
document.getElementById("orangeQty").focus();
return false;
}
else if(!isInteger(document.getElementById("bananaQty").value)){
alert('Please key in an integer in the Banana Quantity text box.');
document.getElementById("bananaQty").value="";
document.getElementById("bananaQty").focus();
return false;
}
else if(document.getElementById("user").value = " "){ /!-Problem, keeps getting repeated-->
document.getElementById("user").focus();
alert('Please key in your name.');
return false;
}
return true;
}
One should use == to compare, = to assign. You are assigning, so add an extra = to compare.
You are using = as assignment, not equivalence. Use == or ===.
Two problems:
Use == for comparison, not =.
The empty string is "", not " ". Notice the extra space.
Result:
else if (document.getElementById("user").value == "") {