- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 10:40 AM
I am trying to verify that a Service Catalog field entry is restricted to exactly a three digit value (i.e. 051, 789, etc). So I am trying to write a Catalog Client Script using Regular Expressions, but it is not working. I keep getting an error message that says "There is a JavaScript error in your browser console" in the Service Portal when I enter an invalid entry in that field, like "AAAAA".
I tried two different methods, but neither one worked. Here is the first:
Here is my second attempt, which is very similar to one we wrote for a Variable Set some time ago, but this one isn't working either.
Can anyone see what my issue is?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 11:12 AM
Thanks for the attempts. Playing around, we finally figured out something that works. We used this code to restrict it to numbers only:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var number = g_form.getValue('pbp_id');
if(!number.match(/^\d{3}/)){
alert('Please enter only a 3 digit number');
g_form.setValue('pbp_id','');
}
}
And then we used the following Variable Attributes to restrict the length,
max_length=3,min_length=3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 10:57 AM
You can do this by getting the length of that particular field and put restriction on basis of that.
Please mark my answer correct and helpful if my answer helped u. Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 11:01 AM
Hello,
Try with below script and let me know if this works.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue('pbp_id');
var regexp= new RegExp('^[0-9]{3}$');
var s = regexp.test(val);
if(s == 'false')
{
alert('Please enter exact 3 numbers only');
g_form.setValue('pbp_id','');
}
}
Thanks,
Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 11:09 AM
Unfortunately, that didn't seem to work. It didn't return any errors, but it didn't seem to do anything either.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 11:12 AM
Thanks for the attempts. Playing around, we finally figured out something that works. We used this code to restrict it to numbers only:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var number = g_form.getValue('pbp_id');
if(!number.match(/^\d{3}/)){
alert('Please enter only a 3 digit number');
g_form.setValue('pbp_id','');
}
}
And then we used the following Variable Attributes to restrict the length,
max_length=3,min_length=3