logo

NJP

Creating your own Virtual Agent Text Input Format Validators

Import · Jan 11, 2021 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

What is shared in this article, might not be best practice. Just sharing this to explore possibilities, find openings in ServiceNow, and have a mindset that your imagination is your limitation.

Hi there,

When designing Virtual Agent topics, a subject to keep in mind is error handling. For example, if User Inputs are not valid for your Topic. With the Paris-release, ServiceNow added Text Input format validation. A great enhancement, at least User Input Text can now be validated without having to add additional steps to your Topic design!

Though what if, you would like to set up your own Input Validators?

Text input format validation

From the Docs:

"In the Text input control, use the Text Input Format field to specify a text format that is validated when a user enters: email addresses, phone numbers, IP addresses, and URLs. You can also specify a custom text validation, which uses a script to set validation rules and related error messages."

The usage of User input text validators is really basic. On any User Input Text, in the User Input Properties (upper part of the right side column), click the Text Input Format field. This will present you with:

image

Out-of-the-box Text, Email, IP Address, Phone Number, and URL are available as validators. You can also add your own Custom validator on each User Input Text.

Custom validator

On the introduction of the Text input format validation, I quickly started using it, and especially the Custom validator option. Though also quickly asking myself… what can I do to limit myself from doing Custom validators over and over, on several steps in a Topic, in several different Topics. Referencing a function in a Script Include would help already. Though, let's dig a bit further… where do the out-of-the-box Validators come from? Are these embedded in the $conversation-builder page and therefore untouchable, or might these be stored somewhere?

sys_cs_field_script_validator

It took a bit of a search, it's undocumented, though… table sys_cs_field_script_validator is what we are really after! This table holds the out-of-the-box Validators.

image

Looking at the code, the out-of-the-box Validators do use Script Include "VAFieldValidator". You would expand that Script Include, or only use the validator script field. For this article, I just make use of the validator script field.

Integer

There's no Integer Validator. So let's set up a custom one. Looking at the out-of-the-box Validators, the validator script should return a true or false value. If false, the Error Message will be triggered.

Regex to validate an Integer could already be something like:

^\d*$

Translating this into the validator script:

function validate(value) {

    var answer = false;
    if(value.match(/^\d*$/g)) {
        answer = true;
    }

    return answer;

}

And that's it!

Result

After adding the new Conversation Server Field Script Validators record, Integer is available as Tekst Input Format:

image

Triggering this new validator in a Virtual Agent chat would result in:

image

Instead of using the custom option over and over, on several steps in a Topic, in several different Topics, we now have our own custom validator available as a selectable option!

---

Kind regards,

Mark Roethof

ServiceNow Technical Platform Architect @ Quint Technology

1x ServiceNow Developer MVP

1x ServiceNow Community MVP

---

LinkedIn

image

View original source

https://www.servicenow.com/community/virtual-agent-nlu-articles/creating-your-own-virtual-agent-text-input-format-validators/ta-p/2306172