Device settings validation

Only example I can find, in the SDK description, regarding using pattern for validation of device settings is [a-zA-Z].

Has anyone managed to find a pattern for validation of for instance an ip address? Normal regex doesn’t seems to work.

I’m fairly sure that it will use input[pattern], so a valid JS regexp should work, but there may be some challenges regarding proper escaping.

What’s the “normal regex” that you tried?

@robertklep, Thanks for your reply.
After some more testing I actually, with correct escaping, got it to work.

IP address validation:

"pattern": "^(?!0)(?!.*\\.$)((1?\\d?\\d|25[0-5]|2[0-4]\\d)(\\.|$)){4}$"

But unfortunately, no error message is displayed if validation fails, so for a better UX-experience it’s probably better to do the validation in onSettings() instead:

 onSettings(event) {

    // Validate
    const regExIp = new RegExp('^(?!0)(?!.*\\.$)((1?\\d?\\d|25[0-5]|2[0-4]\\d)(\\.|$)){4}$');
    if (!regExIp.test(event.newSettings['host'])) throw new Error('Invalid IP adress!');

1 Like