No spaces or special characters are allowed in the field

Sirri
Tera Guru

Hi All,

 

I have requirement, there is one field "short Name" type is 'string'. we should want to restrict no spaces or special characters are allowed in this field.

 

please let me know how to restrict as per the requirement.

 

Thank you

4 ACCEPTED SOLUTIONS

Jitendra Diwak1
Kilo Sage

Hi @Sirri,

 

This will be achieved by below script by using the onChange client script below:

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

    var shortName = g_form.getValue('description'); // Replace 'description' with the actual field name

    // Regular expression to allow only alphanumeric characters
    var regex = /^[a-zA-Z0-9]+$/;

    if (!regex.test(shortName)) {
        alert('Please enter only character witout any spaces');
        //g_form.setValue('description', oldValue); // Reset the field value
    }
}

 

I tested on my PDI for incident description field

 

Please accept my solution if it works for or thumps up

 

Thanks

Jitendra

Please accept my solution if it works for and thumps up.

View solution in original post

Mark Manders
Mega Patron

The client scripts to allow this are already given. Do think if you want to show a field message as well, so people know upfront they shouldn't use spaces and special characters.

Also, when testing, check if this also works if the first or last character is a space. There have been questions about that in the past (that the client script only checks the text and not what's in front or behind it).

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

Robbie
Kilo Patron
Kilo Patron

Hi @Sirri,

 

This can easily be handled via an onChange Client Script with a regex (Regular Expression).

See the below tried and tested script to implement this.

(Reposting this so my initial and the first response doesn't get lost in the thread)

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

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

    var regex = new RegExp("^[a-zA-Z0-9]*$");
    //alert('regex: ' + regex.test(newValue));
    if (!regex.test(newValue)) {
        g_form.clearValue('your_field_name');
        g_form.showFieldMsg('your_field_name', 'Only alphanumeric characters without any spaces can be entered within this field', 'error');
    }
}

 

View solution in original post

Maddysunil
Kilo Sage

@Sirri 

you can write on change client script on Short Name field

 

// Basic configuration
Name: Restrict Short Name Field
Table: [Your Table Name]
Type: onChange
Field: short_name

// Script
(function executeRule(current, previous /*null when async*/) {

    // Define the regex pattern to match only alphanumeric characters (no spaces or special characters)
    var pattern = /^[a-zA-Z0-9]*$/;

    // Get the value of the short_name field
    var shortName = g_form.getValue('short_name');

    // Check if the value matches the pattern
    if (!pattern.test(shortName)) {
        // Display an error message
        g_form.showFieldMsg('short_name', 'No spaces or special characters are allowed in this field.', 'error');
        
        // Clear the invalid value
        g_form.setValue('short_name', '');
    } else {
        // Clear any previous error messages
        g_form.hideFieldMsg('short_name');
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

5 REPLIES 5

Maddysunil
Kilo Sage

@Sirri 

you can write on change client script on Short Name field

 

// Basic configuration
Name: Restrict Short Name Field
Table: [Your Table Name]
Type: onChange
Field: short_name

// Script
(function executeRule(current, previous /*null when async*/) {

    // Define the regex pattern to match only alphanumeric characters (no spaces or special characters)
    var pattern = /^[a-zA-Z0-9]*$/;

    // Get the value of the short_name field
    var shortName = g_form.getValue('short_name');

    // Check if the value matches the pattern
    if (!pattern.test(shortName)) {
        // Display an error message
        g_form.showFieldMsg('short_name', 'No spaces or special characters are allowed in this field.', 'error');
        
        // Clear the invalid value
        g_form.setValue('short_name', '');
    } else {
        // Clear any previous error messages
        g_form.hideFieldMsg('short_name');
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks