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

File Path Name

Tony Capone
Kilo Expert

I have a single text field on a service catalog form in which a user can enter a full file path. There are two restrictions:

  1. The path must start with "\\". An good example would be \\svrp12345\public\filename
  2. The value between "\\" and "\" cannot contain "nas" in any position so the following values would not be allowed: \\nas123\public\filename or \\123nas\public\filename

Here is the script I put together with "full_file_path_for_the_requested_folder" being the name of the field that captures the full file name path.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var msg = "The file path should start with \\\\";
var str = newValue;
if (str.startsWith('\\\\')){
return true;
}
else;
{
g_form.setValue('full_file_path_for_the_requested_folder', '');
g_form.showErrorBox('full_file_path_for_the_requested_folder', msg, true);
}
}

This script works well in fulfilling the first restriction however, I can enter "\\\" for the start of my file name without getting an error. 

 

Can anyone please assist in correcting my script or suggesting a regex. 

Thank you. 

Tony Capone

 

 

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Tony,

Try the following script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	if (newValue.startsWith('\\\\') && !newValue.startsWith('\\\\\\') && !newValue.includes('nas')) {
        g_form.showFieldMsg('full_file_path', 'full file path is OK.', 'info');
    } else {
        g_form.showFieldMsg('full_file_path', 'full file path is NG.', 'error');
    }
}

execution result:

case 1: \\svrp12345\public\filename

find_real_file.png

case 2: \\\svrp12345\public\filename

find_real_file.png

case 3: \\nas123\public\filename

find_real_file.png

case 4:\\123nas\public\filename

find_real_file.png

You could use use 4 backslashes to bypass this. Additionally 'nas' cannot be used anywhere in the path (not only in the first folder as initially requested). 

Markus Kraus
Kilo Sage

This can be solved with regex only, no need for a client script.

Add a new Variable Validation Regex (Service Catalog > Catalog Variables > Variable Validation Regex) using the following configuration:
Name: Network Path (no NAS)
Regular Expression: ^\\\\((?!nas)[^\\])*(\\[^\\])+$
Regex Flag: Case Insensitive

Now update your variable to include the regex:
find_real_file.png

 

Please let us know if this solved your problem.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Tony,

Changed the script to only look for "nas" in the root directory.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	var g = newValue.replace(/^\\\\/,'').replace(/\\.*/, '');

	if (newValue.startsWith('\\\\') && !newValue.startsWith('\\\\\\') && !g.includes('nas')) {
        g_form.showFieldMsg('full_file_path', 'full file path ' + newValue + ' is OK.', 'info');
    } else {
        g_form.showFieldMsg('full_file_path', 'full file path ' + newValue + 'is NG.', 'error');
    }
}

Execution results:

case 1:\\svrp12345\public\filename

find_real_file.png

case 2: \\\svrp12345\public\filename

find_real_file.png

case 3: \\nas123\public\filename 

find_real_file.png

case 4:\\123nas\public\filename

find_real_file.png