# How to check if a string is numeric or not

**URL:** <https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362>\
**Category:** General\
**Created:** [26 September 2020 00:51 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362 "2020-09-26T00:51:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 September 2020 00:51 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/1 "2020-09-26T00:51:00Z")

</div>

Hi,  
I would like to check if `msg.payload` ( **string** ) is a **number** or not.

If I don't use the `function` node, is there a node that can check this?  
I tried searching in node-red library with keyword like verify, validate but no results.

Please tell me!  
Thanks.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 September 2020 01:43 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/2 "2020-09-26T01:43:39Z")

</div>

`switch` node.

`is of type` from the selection type.

![Screenshot from 2020-09-26 11-42-26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77ab6b6ff320e7c51f1b2b05dd5e98f2c5d179c2.png)

Then add another line to say if it is a number type.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 September 2020 02:07 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/3 "2020-09-26T02:07:42Z")

</div>

If using `msg.payload` ( **string** ) with the value 3, the type of is a string, not a number.

I'm thinking of using a `Change` node or a `Switch` node in combination with the `regular expression`.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 September 2020 02:09 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/4 "2020-09-26T02:09:43Z")

</div>

> [@minhhn0205](#):
>
> I would like to check if `msg.payload` ( **string** ) is a **number** or not.
> 
> If I don't use the `function` node, is there a node that can check this?  
> I tried searching in node-red library with keyword like verify, validate but no results.

I'm confused.

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [26 September 2020 05:48 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/5 "2020-09-26T05:48:08Z")

</div>

Take a look at **isNaN() Function**

> **[JavaScript isNaN() Function](https://www.w3schools.com/jsref/jsref_isnan.asp)**

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 September 2020 07:47 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/6 "2020-09-26T07:47:39Z")

</div>

To check whether the payload is a string containing only digits with an optional +/- you can use a Switch node configured with  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6f6cfbb1f320c015424ff42a0b5df82d4faa9eb.png)

That says that the string must be an optional + or - followed by one or more digits and nothing else. The node will pass valid strings and suppress anything else. If you want a notification of a non number then add an Otherwise case going to output 2.

**[Edit]** If you want to allow a single decimal point then it gets much more complex, so if you want that then a regex may not be the best solution.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 September 2020 07:48 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/7 "2020-09-26T07:48:34Z")

</div>

> [@minhhn0205](#):
>
> I would like to check if `msg.payload` ( **string** ) is a **number** or not.

Here a jsonata expression which you can use in a switch node:

- [https://try.jsonata.org/Rn5TyoG1h](https://try.jsonata.org/Rn5TyoG1h)

```auto
/* checks if payload is a number or a string holding a number (no decimal or blanks !) */
$type(payload)="number" ? true : ( $type(payload)="string" and $match(payload,/^[0-9]+$/))

```

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 September 2020 20:31 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/8 "2020-09-26T20:31:54Z")

</div>

Great, both ways work!!!  
I have learned many things...  
@Colin, @janvda Thank you very much!

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [26 September 2020 21:07 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/9 "2020-09-26T21:07:40Z")

</div>

You might run into some edge cases trying to parse the string yourself, so I'd advise actually using Javascript or JSONata to do the test. These flows are examples.

```auto
[{"id":"136fdf3.4a09421","type":"inject","z":"5e0bf7b3.fc34d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":150,"y":80,"wires":[["83f96ee6.f56a98"]]},{"id":"932a054.d90cb78","type":"inject","z":"5e0bf7b3.fc34d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"a","payloadType":"str","x":150,"y":120,"wires":[["83f96ee6.f56a98"]]},{"id":"1fe64078.48d6","type":"inject","z":"5e0bf7b3.fc34d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.10","payloadType":"str","x":150,"y":160,"wires":[["83f96ee6.f56a98"]]},{"id":"83f96ee6.f56a98","type":"link out","z":"5e0bf7b3.fc34d","name":"","links":["cb123243.5647c","394d8256.6de2fe"],"x":275,"y":120,"wires":[]},{"id":"cb123243.5647c","type":"link in","z":"5e0bf7b3.fc34d","name":"","links":["83f96ee6.f56a98"],"x":355,"y":80,"wires":[["b426a0ce.541b8"]]},{"id":"b426a0ce.541b8","type":"change","z":"5e0bf7b3.fc34d","name":"check","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":80,"wires":[["53a7db7a.7cbda4"]]},{"id":"53a7db7a.7cbda4","type":"debug","z":"5e0bf7b3.fc34d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":80,"wires":[]},{"id":"61651499.efacf4","type":"catch","z":"5e0bf7b3.fc34d","name":"","scope":["b426a0ce.541b8"],"uncaught":false,"x":450,"y":120,"wires":[["b89cbecb.b9b3e8"]]},{"id":"ba32a239.3f7fd","type":"function","z":"5e0bf7b3.fc34d","name":"","func":"msg.payload = + msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":160,"wires":[["a9eb457f.bd7648"]]},{"id":"394d8256.6de2fe","type":"link in","z":"5e0bf7b3.fc34d","name":"","links":["83f96ee6.f56a98"],"x":355,"y":160,"wires":[["ba32a239.3f7fd"]]},{"id":"a9eb457f.bd7648","type":"debug","z":"5e0bf7b3.fc34d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":160,"wires":[]},{"id":"b89cbecb.b9b3e8","type":"debug","z":"5e0bf7b3.fc34d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"error.message","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":120,"wires":[]}]

```

If you really need to avoid the `function` node, then ignore the second flow.

---

<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:** [10 October 2020 21:07 UTC](https://discourse.nodered.org/t/how-to-check-if-a-string-is-numeric-or-not/33362/10 "2020-10-10T21:07:41Z")

</div>

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