Configuring a string field to accept alpha numeric characters and a hyphen

Madhan007
Tera Contributor

Hi all ,
How to configure a  String Field in a form to accept AlphaNumeric characters and  Hyphen Symbol (-) only and exactly should be of 16 characters ?

Please pour your Insights ,

Thanks in Advance !!

1 REPLY 1

Naveed_Hussain
Kilo Guru

Create or Edit the Field:

  • Go to the table where you want to add or modify the field.
  • Create a new field or edit an existing one. Set the field type to "String" and set the maximum length to 16.

Add a Dictionary Attribute:

  • Go to the dictionary entry of the field.
  • Set the "Attributes" field to include a regular expression that enforces the allowed characters and length. For example, you can use a regex attribute:

 

regex=^[a-zA-Z0-9-]{16}$

 

 

Create a Client Script for Additional Validation:

  • Navigate to System Definition > Client Scripts.
  • Create a new Client Script with the following details:
    • Type: onSubmit
    • Table: The table where your field exists
    • UI Type: All

Client Script Code:

  • Add the following script to enforce the validation when the form is submitted

 

(function executeRule(current, g_form, g_user, gs, g_scratchpad) {
    // Replace 'your_field_name' with the actual name of your field
    var fieldName = 'your_field_name';
    var fieldValue = g_form.getValue(fieldName);
    
    // Regular expression to match 16 alphanumeric characters and hyphens
    var regex = /^[a-zA-Z0-9-]{16}$/;
    
    // Check if the field value matches the regex
    if (!regex.test(fieldValue)) {
        // Display an error message and prevent form submission
        g_form.showFieldMsg(fieldName, 'The field must be exactly 16 characters long and can only contain alphanumeric characters and hyphens.', 'error');
        return false; // Prevent form submission
    }
    
    return true; // Allow form submission
})(current, g_form, g_user, gs, g_scratchpad);

 

Save and Test:

  • Save the client script.
  • Test the form by entering values in the field to ensure it only accepts 16-character alphanumeric strings and hyphens.