Virtual Agent User Input format validation [Paris]
Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
Hi there,
When designing Virtual Agent topics, adding any form of validation is completely up to the builder of the topic. One of the validations you could think of, validation of the text input from a user. With the Paris-release, Eureka: a few Text Input format validations have been added! Let's explore the possibilities.
Text input format validation
In the Text input control, you can 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.
If we would select "Email" as Tekst Input Format, and test this in a Topic, the validation would look something like:
So easy and quick to apply!
Custom Text input format validation
The types of text input format validations are limited out-of-the-box. Though there's also an option "custom". So actually, you could create unlimited custom validations yourself. This can be done by selecting custom as an option and using the script capability.
The example script contains the below code, which gives us a good indication of what to return:
return {
valid: isValid,
error: isValid ? undefined : "Sorry, email must be 'foo@bar.com'"
};
So what if we would like to validate if the Text input is an integer? What jumps in mind for me is applying a RegEx:
^\d*$
Wrapping this into the example code, the script could look something like below. Do note that I also applied the usage of gs.getMessage for Localization purposes.
function validate(value) {
var isValid = value.match(new RegExp('^\\d*$'));
return {
valid: isValid,
error: isValid ? undefined : gs.getMessage("Invalid integer format")
}
}
Result
Testing this custom Text input format validation, the validation would look something like:
So nice! Validations like these could enhance your Virtual Agent topics a lot, making them more robust or making your topics a lot shorter because there is less need for additional utilities that validate the format text inputs. As mentioned, the out-of-the-box validations are limited, though you can add your own custom validations. Add these for example to a Script Include which you can call from the script field on the Text Input Format, so you don't have to write the code over and over, and only have to maintain the code in one place in your ServiceNow instance.
---
And that's it actually. Hope you like it. If any questions or remarks, let me know!
Kind regards,
Mar k Roethof
ServiceNow Technical Consultant @ Quint Technology
1x ServiceNow Developer MVP
1x ServiceNow Community MVP
---
https://www.servicenow.com/community/virtual-agent-nlu-articles/virtual-agent-user-input-format-validation-paris/ta-p/2310415