# RED.validators.regex() custom message?

**URL:** https://discourse.nodered.org/t/red-validators-regex-custom-message/95116
**Category:** Developing Nodes
**Created:** [4 February 2025 16:46 UTC](https://discourse.nodered.org/t/red-validators-regex-custom-message/95116 "2025-02-04T16:46:35Z")
**Posts on this page:** 4
**Page:** 1

<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: [4 February 2025 16:46 UTC](https://discourse.nodered.org/t/red-validators-regex-custom-message/95116/1 "2025-02-04T16:46:35Z")

</div>

I'm using something like this to validate a colour picker input -

stroke: { value: "#ff0000", validate: RED.validators.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) }

I did a bit of digging around on the forum and it seems to be a common issue, that the red highlighting is applied to a hidden object.

I tried (unsuccessfully) to target the button border  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d1551e7db7eea91f094c7b7ae93e51704da5a95.png)

Failing that I vaguely remember reading that it is possible to pass some options to the validator function so that - invalid input pattern can be replaced by something more helpful ?

I cannot find anywhere now, so I may have dream it 😉

Is this possible to change this message somehow ?

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [4 February 2025 17:08 UTC](https://discourse.nodered.org/t/red-validators-regex-custom-message/95116/2 "2025-02-04T17:08:51Z")

</div>

> [@Sean-McG](#):
>
> I did a bit of digging around on the forum and it seems to be a common issue, that the red highlighting is applied to a hidden object.
> 
> I tried (unsuccessfully) to target the button border

I'm not aware of a common issue here. Can you raise an issue on github? The color picker should highlight errors properly - that's for us to fix.

It is not possible to customise the messages returned by the validator - but you could wrap it with your own code to do so. Something like... (untested, but I think should do it...):

```auto
validate: (function () {
   const validateFunction = RED.validators.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);
   return function (v, opt) {
      const result = validateFunction(v,opt);
      if (typeof result === 'string') {
         return 'my custom error message'
      }
      return result
   }
})()
```

---

<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: [4 February 2025 17:36 UTC](https://discourse.nodered.org/t/red-validators-regex-custom-message/95116/3 "2025-02-04T17:36:03Z")

</div>

Thanks for getting back Nick.

Perhaps common is a bit strong 😉 but there were a few along similar lines -

> [@typedInput - enforcing only string output from JSONata type](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393):
>
> Hi Node-RED brains-trust. I need to validate a typedInput that allows JSONata BUT I need the result to be a string not JSON. I can do this with a validate function: const f = value.slice(0, 1) return !( f === '{' || f === '[') Which does mark the node as invalid HOWEVER, when I open the config panel for the node, it does NOT highlight the field in error. [image] This is in error for example and so should be highlighted in red. How can I do this? I see that there is a RED.validate.typedInp…

As you probably noticed I'm working a souped-up version of your annotate node.

As there is the possibility to directly type a hex value into the colour picker, I though it best to validate this. However although it marks an error on the node, the input element that gets the error class is a hidden object.

The code you shared should be a least a help to know which picker has the incorrect value, thanks.

---

<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: [5 April 2025 17:36 UTC](https://discourse.nodered.org/t/red-validators-regex-custom-message/95116/4 "2025-04-05T17:36:04Z")

</div>

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