# How can I test for any text received?

**URL:** <https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334>\
**Category:** General\
**Created:** [8 February 2024 18:13 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334 "2024-02-08T18:13:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [8 February 2024 18:13 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/1 "2024-02-08T18:13:54Z")

</div>

Hi,

I want to test for any text received in my **function** , this will then turn on a relay until no text received.  
I tried below, but does not work. What do I need to put in the **if (msg.payload === "")** part to match any text ?

```auto

if (msg.payload === "")
{
  msg.payload = "RLY = ON";
  return msg;
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [8 February 2024 18:23 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/2 "2024-02-08T18:23:30Z")

</div>

Maybe check if payload is string and if payload.length \> 0 .

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 February 2024 18:44 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/3 "2024-02-08T18:44:48Z")

</div>

Try

```auto
if (msg.payload && typeof msg.payload === "string")
{
  msg.payload = "RLY = ON";
  return msg;
}

```

Truthy [Truthy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)

typeof [typeof - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)

An array has length property to.

example

```auto
[{"id":"a443d98b41ad5fd3","type":"inject","z":"d1395164b4eec73e","name":"trina with length and truthy","props":[{"p":"payload"},{"p":"separater","v":"\\","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"a strina with length and truthy","payloadType":"str","x":450,"y":4740,"wires":[["3b9b60abd878fb12"]]},{"id":"3b9b60abd878fb12","type":"function","z":"d1395164b4eec73e","name":"function 153","func":"if (msg.payload && typeof msg.payload === \"string\")\n{\n msg.payload = \"RLY = ON\";\n return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"path","module":"path"}],"x":690,"y":4760,"wires":[["1f8ae278cd39375c"]]},{"id":"99adde23cb42603b","type":"inject","z":"d1395164b4eec73e","name":"stinng no length and falsey","props":[{"p":"payload"},{"p":"separater","v":"\\","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":450,"y":4800,"wires":[["3b9b60abd878fb12"]]},{"id":"1f8ae278cd39375c","type":"debug","z":"d1395164b4eec73e","name":"debug 2470","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":4740,"wires":[]}]

```

[edit] added missing example.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [8 February 2024 18:50 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/4 "2024-02-08T18:50:29Z")

</div>

> [@JeromeVISUALS](#):
>
> payload.length \> 0

I used **JeromeVISUALS** suggestion as this is for outputting of speech synthesis so it could be text or numbers that are recieved. The relay turns on an amplifier to hear the speech sent.

```auto
if (msg.payload.length > 0 )
{
  msg.payload = "RLY = ON";
  return msg;
}
  

```

Thanks for your help both of you 👍

---

<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:** [8 February 2024 18:51 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/5 "2024-02-08T18:51:02Z")

</div>

If still not working use a debug node to show us what is in the payload when no text is received.

---

<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:** [8 May 2024 18:51 UTC](https://discourse.nodered.org/t/how-can-i-test-for-any-text-received/85334/6 "2024-05-08T18:51:16Z")

</div>

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