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

Spaces are auto trimmed in Single Line text variable

Anant Samaiya
Tera Contributor

We have requirement where if a user enters any space or special characters in "Single Line Text variable" then we need to show a alert message saying "Spaces and Special Characters not Allowed."

However I am able to handle the special characters and inBetween spaces. The issue is with Leading and Trailing spaces. It seems the "Single Line Text variable" ignores the leading and trailing spaces or there is backend TRIM() running on it hence I am not able to deal with it.

 

Anyone with any Idea to Cop with above issue.

3 REPLIES 3

Mark Manders
Mega Patron

What's your script looking like? 

I tried a quick one on my pdi with this and it seems to work:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    var regex = /^[^\s].*[^\s]$|^[^\s]$/;
    if (!regex.test(newValue) || /[^a-zA-Z0-9]/.test(newValue)) {
        alert('Spaces and Special Characters are not allowed.');
    }
}

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

Community Alums
Not applicable

Hi @Anant Samaiya ,

You can use OnChange client script on Requested Item table for the particular field which you are looking at and apply this script.

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

// Regular expression to match only numbers and alphabets
var regex = /^[A-Za-z0-9]+$/;

if (!newValue.match(regex)) {
alert("Please enter only numbers and alphabets.");
g_form.setValue(control.name, oldValue); // Reset the field value to the old value
}
}

Mark my Answer Helpful or Accept Solution if it resolves your issue.

Community Alums
Not applicable

Hi @Anant Samaiya ,

You can use OnChange client script for that particular field which you are looking-

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

// Regular expression to match only numbers and alphabets
var regex = /^[A-Za-z0-9]+$/;

if (!newValue.match(regex)) {
alert("Please enter only numbers and alphabets.");
g_form.setValue(control.name, oldValue); // Reset the field value to the old value, if you need
}
}

Mark my Answer Helpful or Accept Solution if it resolves your issue.