String field Contains integer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2022 03:50 AM
Is there a way to validate if a string contains Integer ?
Examples of data in variable (Need to enable/disable a check box based on string contains integer):
34567 9/4/2022
INC4312 9/4/2022
These 2 examples should be validated as contains integer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2022 05:18 AM
Hi @Kumar
You can use the below code. I have tested it in the background Script it returns 'true' if the string has a number and false if it is a pure string.
function hasNumber(getString) {
return /\d/.test(getString);
}
var a=hasNumber('INC4312 9/4/2022');
gs.info(a);
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Gunjan Kiratkar
Consultant - ServiceNow, Cloudaction
Rising Star 2022
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2022 08:18 PM
Hi
solution given by Gunjan is a good approach, but you also would get a hit when having only date occurrences like "9/4/2022"
Unfortunately, you left some important rules and restrictions so that I only can assume, that formal numbers have at least 5 digits (this is important to have a distinction to normal year numbers). Furthermore it is not clear whether the given text is multiline or not.
At https://regex101.com/r/Mj75aB/1 I have created a regular expression which works fine as it only recognizes the allowed strings and ignores all other variants - based on my assumptions.
Kind regards
Maik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2022 05:06 AM
Hi Kumar,
I may not be understanding the question too well. Is the question about checking the string in bold and setting checkbox if it contains a string? If so, try the following script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var fieldList = newValue.split(' ');
g_form.setValue('checkbox', /\d/.test(fieldList[0])); // check box is named "checkbox"
}
Execution result:
case 1: If contains integer
case 2: Does not contain integer