
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
09-27-2020 10:40 PM - edited 08-16-2024 10:36 PM
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!
C |
If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.
Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in? |
Kind regards,
Mark Roethof
ServiceNow Technical Consultant @ Quint Technology
1x ServiceNow Developer MVP
1x ServiceNow Community MVP
---
- 2,806 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Mark Roethof I got a doubt, how to limit certain characters in the validation regex?
var isValid = value.match(new RegExp('^\\d*$'));
For the above line of code you have given an example of how to check what needs to be included in the field, but how to do it if we want to exclude certain expressions in the email field? please give some inputs on this.