Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Restriction validation for single line text field

Sirri
Tera Guru

Hi All,

 

Please let me know how to apply these all conditions. I have one single line text field ( Name of the project).

 

I should want to restrict that field as per the below requirement:

Name of the project  can't contain special characters, such as / : \ ~ & % ; @ ' " ? < > | # $ * } { , + = [ ], can't begin with an underscore, can't begin or end with a period, and must be 64 or fewer characters.

 

Please provide the code.

 

Thank you.

2 ACCEPTED SOLUTIONS

Gangadhar Ravi
Giga Sage

@Sirri You can do onSubmit or onChange script below is sample script.

 

function onSubmit() {
  var projectName = g_form.getValue('name_of_project'); // Replace 'name_of_project' with your field name.

  // Disallowed special characters.
  var specialCharPattern = /[\/:\\~&%';"<>|#\$\*\}\{\[\],+=]/;

  // Check for special characters.
  if (specialCharPattern.test(projectName)) {
    g_form.showErrorBox('name_of_project', 'Project name cannot contain special characters like / : \\ ~ & % ; @ \' " ? < > | # $ * } { , + = [ ]');
    return false;
  }

  // Check if the string starts or ends with a period or underscore.
  var regex = /^[a-zA-Z0-9][a-zA-Z0-9\s\-._]{0,62}[a-zA-Z0-9]$/;

  if (!regex.test(projectName)) {
    g_form.showErrorBox('name_of_project', 'Invalid project name. Project name must be alphanumeric, cannot start with underscore or period, and cannot contain special characters.');
    return false;
  }

  return true;
}

View solution in original post

Sid_Takali
Kilo Patron

Hi @Sirri Try below onChange Client Script 

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

    var projectNamePattern = /^[^_\s][^\/:\~\&\%\;\@\^\{\}\[\]\<\>\|\#\$\*\?\'\"\+\=\,\.]{1,63}[^\.]$/;
    if (!projectNamePattern.test(newValue)) {
        g_form.showFieldMsg('u_name_of_the_project', 'Name of the project cannot contain special characters, cannot begin with an underscore, cannot begin or end with a period, and must be 64 or fewer characters.', 'error');
        g_form.setValue('u_name_of_the_project', oldValue); 
    } else {
        g_form.hideFieldMsg('u_name_of_the_project');
    }
}

View solution in original post

3 REPLIES 3

Gangadhar Ravi
Giga Sage

@Sirri You can do onSubmit or onChange script below is sample script.

 

function onSubmit() {
  var projectName = g_form.getValue('name_of_project'); // Replace 'name_of_project' with your field name.

  // Disallowed special characters.
  var specialCharPattern = /[\/:\\~&%';"<>|#\$\*\}\{\[\],+=]/;

  // Check for special characters.
  if (specialCharPattern.test(projectName)) {
    g_form.showErrorBox('name_of_project', 'Project name cannot contain special characters like / : \\ ~ & % ; @ \' " ? < > | # $ * } { , + = [ ]');
    return false;
  }

  // Check if the string starts or ends with a period or underscore.
  var regex = /^[a-zA-Z0-9][a-zA-Z0-9\s\-._]{0,62}[a-zA-Z0-9]$/;

  if (!regex.test(projectName)) {
    g_form.showErrorBox('name_of_project', 'Invalid project name. Project name must be alphanumeric, cannot start with underscore or period, and cannot contain special characters.');
    return false;
  }

  return true;
}

Sid_Takali
Kilo Patron

Hi @Sirri Try below onChange Client Script 

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

    var projectNamePattern = /^[^_\s][^\/:\~\&\%\;\@\^\{\}\[\]\<\>\|\#\$\*\?\'\"\+\=\,\.]{1,63}[^\.]$/;
    if (!projectNamePattern.test(newValue)) {
        g_form.showFieldMsg('u_name_of_the_project', 'Name of the project cannot contain special characters, cannot begin with an underscore, cannot begin or end with a period, and must be 64 or fewer characters.', 'error');
        g_form.setValue('u_name_of_the_project', oldValue); 
    } else {
        g_form.hideFieldMsg('u_name_of_the_project');
    }
}

Hi Sid,

 

Please let me know why used test in the if condition as per the below.

if (!projectNamePattern.test(newValue))

Thank you