- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 05:55 AM
I modifying a module we created in Service Now and I've added two integer fields one of which I want to restrict to 2 characters and the other to four, additionally I want the four character field to display a 1234 and not 1,234.
Can anyone suggest an easy way to do this please as I can't track it down in the WIKI?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 07:06 AM
Hi Angus,
You don't need the number validation if you're using an integer field as the dictionary entry already validates that, so we only need to worry about the length. Here's an onchange script that returns an alert and clears out the value of the field if it's not 4 digits.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if (newValue.toString().length != 4) {
alert("Please enter a 4 digit number");
g_form.setValue('fieldname', '');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 06:29 AM
Hi Angus,
This code can works . try this.
var val = '123';
var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;
if (re.test(val))
{
if(numb.length == 4)
{
}
else
{
alert('Please entnter exactly 4 digits');
return false;
}
}
else
{
alert('Please enter only numbers')
}
Regards,
Harish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 07:01 AM
Harish I made the change you suggested however I get an error as follows:-
Javascript parse error as line (31) column (21) problem = invalid return
My script is:-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
}
var val = '123';
var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;
if (re.test(val))
{
if(numb.length == 4)
{
}
else
{
alert('Please entnter exactly 4 digits');
return false;
}
}
else
{
alert('Please enter only numbers')
}
The error appears to be on the line that includes return false;
I've tried to debug it to no avail, any suggestions?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 07:06 AM
Hi Angus,
You don't need the number validation if you're using an integer field as the dictionary entry already validates that, so we only need to worry about the length. Here's an onchange script that returns an alert and clears out the value of the field if it's not 4 digits.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if (newValue.toString().length != 4) {
alert("Please enter a 4 digit number");
g_form.setValue('fieldname', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 07:18 AM
Perfect Brad.
Many thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2014 07:19 AM
try no
function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
if (isLoading || newValue == '')
{
return;
}
else
{
var val = newValue;
var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
if (re.test(val))
{
if(val.length == 4)
{
}
else
{
alert('Please entnter exactly 4 digits');
return false;
}
}
else
{
alert('Please enter only numbers');
}
}
}