The CreatorCon Call for Content is officially open! Get started here.

String field to accept name in given format

Vijay Baokar
Kilo Sage

Hi Folks,

 

I have a requirement which is on custom table where i have a name field that should accept value that starts with "IND" space "Region" space "Code"

so the expected result should be IND Region 001 or IND Region 002

IND and Region are static where as Code is dynamic. We have 5 codes. how can we fetch codes from a variable or an Array to get expected result.

 

i am trying with below code:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   
var value = g_form.getValue('u_name_format'); // Replace with your field name


    // Define the expected pattern: starts with "IND", then "Region", then "Code"
    var pattern = /^IND.*Region.*Code.*/;
alert(pattern);
    if (!pattern.test(value)) {
       
        g_form.showFieldMsg('u_name_format', 'Value must start with "IND", followed by "Region" and then "Code".', 'info');
        g_form.setValue('u_name_format', ''); // Optional: clear the field
    } else {
        g_form.hideFieldMsg('u_name_format', true);
    }


   //Type appropriate comment here, and begin script below
   
}
 
1 ACCEPTED SOLUTION

Nawal Singh
Giga Guru

Hi @Vijay Baokar ,

 

Please review below code-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get value from the field
    var value = g_form.getValue('u_name_format');

    // Define allowed codes
    var allowedCodes = ['001', '002', '003', '004', '005'];

    // Build dynamic regex pattern
    var codePattern = allowedCodes.join('|'); // e.g., "001|002|003|004|005"
    var pattern = new RegExp('^IND Region (' + codePattern + ')$');

    // Validate the input
    if (!pattern.test(value)) {
        g_form.showFieldMsg('u_name_format', 'Value must be in the format "IND Region XXX", where XXX is one of: ' + allowedCodes.join(', '), 'error');
        g_form.setValue('u_name_format', ''); // Optional: clear the invalid value
    } else {
        g_form.hideFieldMsg('u_name_format', true);

        //  Extract code part
        var code = value.split(' ')[2]; // Get the "XXX" part

        // Set values to other fields (example)
        g_form.setValue('u_region_code', code); // Sets another field (e.g., u_region_code) with the code
        g_form.setValue('u_region_name', 'India Region ' + code); // Custom text, optional
    }
}

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

View solution in original post

4 REPLIES 4

Nawal Singh
Giga Guru

Hi @Vijay Baokar ,

 

Please review below code-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get value from the field
    var value = g_form.getValue('u_name_format');

    // Define allowed codes
    var allowedCodes = ['001', '002', '003', '004', '005'];

    // Build dynamic regex pattern
    var codePattern = allowedCodes.join('|'); // e.g., "001|002|003|004|005"
    var pattern = new RegExp('^IND Region (' + codePattern + ')$');

    // Validate the input
    if (!pattern.test(value)) {
        g_form.showFieldMsg('u_name_format', 'Value must be in the format "IND Region XXX", where XXX is one of: ' + allowedCodes.join(', '), 'error');
        g_form.setValue('u_name_format', ''); // Optional: clear the invalid value
    } else {
        g_form.hideFieldMsg('u_name_format', true);

        //  Extract code part
        var code = value.split(' ')[2]; // Get the "XXX" part

        // Set values to other fields (example)
        g_form.setValue('u_region_code', code); // Sets another field (e.g., u_region_code) with the code
        g_form.setValue('u_region_name', 'India Region ' + code); // Custom text, optional
    }
}

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Hi @Nawal Singh Thanks for the response. I have tried your code but its giving me the same result as mine , i mean its executing "

g_form.setValue('u_name_format', ''); 

always clearing the value without any fieldMsg. However if i give correct value as "IND Region 002" its accepting but if format is incorrect then msg is not coming.

Hi @Nawal Singh i have moved 

g_form.showFieldMsg

after  

g_form.setValue('u_name_format', '')

now its working. Thanks.

Ankur Bawiskar
Tera Patron
Tera Patron

@Vijay Baokar 

Glad to know that you got solution.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader