# Problem using isNaN()

**URL:** <https://discourse.nodered.org/t/problem-using-isnan/79496>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 June 2023 23:43 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496 "2023-06-29T23:43:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![amineDH](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@amineDH](https://discourse.nodered.org/u/amineDH)\
**Post date:** [29 June 2023 23:43 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/1 "2023-06-29T23:43:28Z")

</div>

```auto
function only_num(s) {
    for (const character of s) {
        if (isNaN(character)) {
            return false;
        }
    };
    return true;
};
var space_removed = msg.packet.replace(" ", '');
return {
    "packet": msg.packet,
    "space_removed": space_removed,
    "only_num(space_removed)": only_num(space_removed),
};

```

I used this code inside a function node with a msg.packet "57 029" and this was the output.  
It is supposed to be true, not false.  
 ![ScreenShot_20230630003947](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a4018b0a2f1a7842f53dc190d364700d1b621279.png)  
can anyone explain why ??  
I just need to check if a string contains only numbers

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [30 June 2023 00:23 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/2 "2023-06-30T00:23:24Z")

</div>

I'm using this [STRING node](https://flows.nodered.org/node/node-red-contrib-string) it fits most of my requirements ...should fit your ask for trim and return numbers only

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [30 June 2023 02:39 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/3 "2023-06-30T02:39:08Z")

</div>

It works OK for me using your code ?

packet: "57 029"  
space\_removed: "57029"  
only\_num(space\_removed): true  
\_msgid: "70af63ef526e09be"

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [30 June 2023 03:09 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/4 "2023-06-30T03:09:37Z")

</div>

yeah ... and thats how it would look like with the STRING node 😉

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/032897a32434bb3935a64c8dfc141154c4320c28.png)

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [30 June 2023 03:17 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/5 "2023-06-30T03:17:13Z")

</div>

to check if content is numeric only ... see below 😉 very easy

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3431f5d0d05dd5f9a1e9b616749c5107b27cb3a.png)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [30 June 2023 14:20 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/6 "2023-06-30T14:20:42Z")

</div>

Its a very versatile node I use it myself.

But doesn't help explain why OP code works for OK when I tested it.

@amineDH Here is a modified flow using regex, give this a try

```auto
[
    {
        "id": "7f389018cf7f8f27",
        "type": "function",
        "z": "be14baae.1728",
        "name": "function 29",
        "func": "function only_num(space_removed) {\n return /^\\d+$/.test(space_removed);\n}\n\nlet space_removed = msg.packet.replace(/\\s/g, ''); // Remove white space from packet\n\nreturn {\n \"packet\": msg.packet,\n \"space_removed\": space_removed,\n \"only_num(space_removed)\": only_num(space_removed),\n};\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 3480,
        "wires": [
            [
                "f1939185c12e2bd9"
            ]
        ],
        "info": "function only_num(space_removed) {\r\n return /^\\d+$/.test(space_removed);\r\n}\r\n\r\nlet space_removed = msg.packet.replace(/\\s/g, ''); // Remove white space from packet\r\n\r\nreturn {\r\n \"packet\": msg.packet,\r\n \"space_removed\": space_removed,\r\n \"only_num(space_removed)\": only_num(space_removed),\r\n};\r\n\r\n"
    },
    {
        "id": "a5efb86805716da8",
        "type": "inject",
        "z": "be14baae.1728",
        "name": "",
        "props": [
            {
                "p": "packet",
                "v": "57 029",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 635,
        "y": 3480,
        "wires": [
            [
                "7f389018cf7f8f27"
            ]
        ]
    },
    {
        "id": "f1939185c12e2bd9",
        "type": "debug",
        "z": "be14baae.1728",
        "name": "debug 319",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 955,
        "y": 3480,
        "wires": []
    },
    {
        "id": "ee75475c57de0c19",
        "type": "inject",
        "z": "be14baae.1728",
        "name": "",
        "props": [
            {
                "p": "packet",
                "v": "A 57 029",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 635,
        "y": 3420,
        "wires": [
            [
                "7f389018cf7f8f27"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![amineDH](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@amineDH](https://discourse.nodered.org/u/amineDH)\
**Post date:** [1 July 2023 15:22 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/7 "2023-07-01T15:22:51Z")

</div>

Thank you very much,  
but I've tried this solution and Nexus's solution and still the same problem.

 ![ScreenShot_20230701161520](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f625ee6b67b484010ac61457df25246a4df7490.png)

I tested the string node with isNumeric() method.

![ScreenShot_20230701161837](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10c0af874e729e1e695ba98c291b43f84271af64.png)

The first result is from the "lora-packet" and the second is from the inject node, same packet but different result.  
This is my lora-packet function node, is there any problem with it ???

```auto
const lora_packet = loraPacket;
try {
    var originalPacket = msg.payload.col16;
    var modifiedPacket = originalPacket.replace(/-/g, "");

    const packet = lora_packet.fromWire(Buffer.from(modifiedPacket, "hex"));
    const NwkSKey = Buffer.from("0D8063CDAEE6450073A7BB93541D068D", "hex");
    const AppSKey = Buffer.from("1B3EF833E8DA2D2845B9E3A8CCDA4ABC", "hex");

    //console.log("Decrypted (ASCII)='" + lora_packet.decrypt(packet, AppSKey, NwkSKey).toString() + "'");
    if (packet.DevAddr == "&�F") {
        return {
            "time": msg.payload.col3,
            "packet": lora_packet.decrypt(packet, AppSKey, NwkSKey).toString()
        };
    }
    else {
        return {};
    }
} catch (error) {}

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [1 July 2023 17:01 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/8 "2023-07-01T17:01:04Z")

</div>

My best guess is that may be something different in the lora packet we cannot see in the debug?

Can you capture the complete message before it goes into your lora function node, then I can test with it.

---

<div class="post-metadata">

**Author:** ![amineDH](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@amineDH](https://discourse.nodered.org/u/amineDH)\
**Post date:** [1 July 2023 17:21 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/9 "2023-07-01T17:21:36Z")

</div>

I'm reading data from a csv file.

![ScreenShot_20230701181659](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23fa114e6abf8781c4be020fb74dd976e2516510.png)

and this is a message example from the CSV convertor

```auto
{"filename":"C:\\Users\\dhami\\Desktop\\pktlog_AA555A0000000101_20230623T162117Z.csv",
"payload":{
"col1":"AA555A0000000101",
"col3":"2023-06-23 16:58:11.202Z",
"col4":1522195652,
"col5":868300000,
"col6":1,"col7":1,
"col8":"CRC_BAD",
"col9":21,
"col10":"LORA",
"col11":125000,
"col12":"SF12 ",
"col13":"05-Apr",
"col14":-21,
"col15":8,
"col16":"C046DA0B-33891A03-1507FE33-AD04F717-59E138C8-B3"},
"parts":{"index":126,"ch":"\n","type":"string","id":"d9ec479b5f811bbb","count":null},
"_msgid":"3157f6e045e3a6b6",
"columns":"col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16"}

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [1 July 2023 17:27 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/10 "2023-07-01T17:27:03Z")

</div>

Can you please post again using this guide so people can copy paste.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![amineDH](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@amineDH](https://discourse.nodered.org/u/amineDH)\
**Post date:** [2 July 2023 15:13 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/11 "2023-07-02T15:13:59Z")

</div>

thanks, pretty helpful .  
i hope the my edit fixes it

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [3 July 2023 01:02 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/12 "2023-07-03T01:02:21Z")

</div>

I'm away at the moment so cannot test this.

Logically same input = same output.  
As this is not the case, it cannot be the same input, even though it looks like it in debug.

Can you try writing the output from "lora-packet" to a file and open with notepad++ and look for any odd none printable characters ?

---

<div class="post-metadata">

**Author:** ![cameo69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cameo69/32/66593_2.png) [@cameo69](https://discourse.nodered.org/u/cameo69)\
**Post date:** [3 July 2023 13:48 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/13 "2023-07-03T13:48:08Z")

</div>

Adding 0x00 to the string would fail the function. And probably any other non-printable character would too.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d6293e29e6b10f365865c2b45645b1a824c93997.png)

```auto
[{"id":"5f8f404c8f069d15","type":"inject","z":"d2d96229134f29f0","name":"","props":[{"p":"packet","v":"57 029","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":770,"y":140,"wires":[["543cf8fd9d4bf86e"]]},{"id":"98aa909eef4126e1","type":"debug","z":"d2d96229134f29f0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":140,"wires":[]},{"id":"70d5cc0991a558fa","type":"function","z":"d2d96229134f29f0","name":"","func":"function only_num(s) {\n for (const character of s) {\n if (isNaN(character)) {\n return false;\n }\n };\n return true;\n};\nvar space_removed = msg.packet.replace(\" \", '');\nreturn {\n \"packet\": msg.packet,\n \"space_removed\": space_removed,\n \"only_num(space_removed)\": only_num(space_removed),\n};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1100,"y":140,"wires":[["98aa909eef4126e1"]]},{"id":"543cf8fd9d4bf86e","type":"function","z":"d2d96229134f29f0","name":"add 0x00","func":"msg.packet = msg.packet + String.fromCharCode(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":140,"wires":[["70d5cc0991a558fa"]]},{"id":"18f2869a5b932066","type":"inject","z":"d2d96229134f29f0","name":"","props":[{"p":"packet","v":"57 029","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":770,"y":100,"wires":[["70d5cc0991a558fa"]]}]

```

---

<div class="post-metadata">

**Author:** ![amineDH](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@amineDH](https://discourse.nodered.org/u/amineDH)\
**Post date:** [3 July 2023 15:36 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/14 "2023-07-03T15:36:00Z")

</div>

I've tried printing it to a file, and eventually I found out that there are null characters attached to the string.

```auto
{"col1":"54 029\u0000\u0000"}
{"col1":"55 027\u0000\u0000"}
{"col1":"56 029\u0000\u0000"}

```

So I just used this to fix it, and my project works fine now.

```auto
.replace(/\u0000/g, '');

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 July 2023 15:36 UTC](https://discourse.nodered.org/t/problem-using-isnan/79496/15 "2023-07-17T15:36:07Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
