String field Contains integer

Kumar38
Kilo Sage

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.

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

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

Maik Skoddow
Tera Patron
Tera Patron

Hi @Aanand Kumar 

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

Hitoshi Ozawa
Giga Sage
Giga Sage

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

find_real_file.png

case 2: Does not contain integer

find_real_file.png