How to check if a string is numeric or not

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.

switch node.

is of type from the selection type.

Screenshot from 2020-09-26 11-42-26

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

1 Like

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.

I'm confused.

Take a look at isNaN() Function

2 Likes

To check whether the payload is a string containing only digits with an optional +/- you can use a Switch node configured with
image

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.

1 Like

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

/* 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]+$/))
1 Like

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

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.

[{"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.

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