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

How to limit string to specific values

Alon Grod
Tera Expert

Hi,

 

I have the field 'name' of type string in my catalog item. I need to write on change client script on this field.

The user can type only these combination of characters:

 

1) small english letters

2) numbers

3) he can use only the special symbol '-'  (e.g Alon-)

4) not more than 64 characters in total

 

* If the users does not meet these condition, he needs to see an error and he cant submit the catalog item until he fix the value that was entered.

 

Can anyone plesae help

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage

@Alon Grod ,

 

Please check this solution if this is worked for you!

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
g_form.clearMessages();
var pattern = /^[a-z0-9-]*$/;
if (newValue.length > 64 || !(pattern.test(newValue))) {
g_form.addErrorMessage("Please enter a valid string with number,small character and '-' as the special character and value should be less than 64 characters");
g_form.clearValue('Your_field_backend_name'); // please mention your field name here
}
}

 

 

Tried in my dev instance worked for me!

 

PRINCE_ARORA_0-1678903482410.png

 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact. 

View solution in original post

3 REPLIES 3

Sai Shravan
Mega Sage

Hi @Alon Grod ,

 

Below is the on submit client script you can try:

function onSubmit() {
  var name = g_form.getValue('name');
  var regex = /^[a-z0-9-]{1,64}$/;
  if (!regex.test(name)) {
    alert('Name can only contain small english letters, numbers and the symbol "-".');
    return false; // prevent submission
  }
  return true; // allow submission
}

 

This script checks if the field value matches the regular expression ^[a-z0-9-]{1,64}$, which means:

^ start of string
[a-z0-9-] any small english letter, number or '-' symbol
{1,64} the string can be between 1 and 64 characters long
$ end of string

Regards,

Shravan.
Please mark this as helpful and correct answer, if this helps you 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Prince Arora
Tera Sage

@Alon Grod ,

 

Please check this solution if this is worked for you!

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
g_form.clearMessages();
var pattern = /^[a-z0-9-]*$/;
if (newValue.length > 64 || !(pattern.test(newValue))) {
g_form.addErrorMessage("Please enter a valid string with number,small character and '-' as the special character and value should be less than 64 characters");
g_form.clearValue('Your_field_backend_name'); // please mention your field name here
}
}

 

 

Tried in my dev instance worked for me!

 

PRINCE_ARORA_0-1678903482410.png

 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact. 

@Alon Grod ,

 

Did you get the chance to look into the solution?