RegEx on catalog item variable

dev_K
Tera Contributor

Hi!

 

 

I am trying to make sure that users are submitting values that have "MC" prefix eg. MCtest123.

 

Here is the regex I created:

 

dev_K_0-1713860164364.png

 

here, in the variable details I have added 'MC' to automatically populate the field:

 

dev_K_1-1713860237531.png

 

 

however when i type sth here Im getting an error:

 

 

dev_K_2-1713860389271.png

and next when i remove what i have added before, same error reappears:

 

dev_K_3-1713860436279.png

 and the error message disappears when I remove the entire line including "MC"

 

 

 

4 REPLIES 4

Sohail Khilji
Kilo Patron
Kilo Patron

I see the base type name is string field. Why not simply create a onchange client script which just accepts normal text string from the user, lets say 'Test', Your onchange client script will change the value to concat ''Test'' with prefix MC. 

 

if (!newValue.startsWith('MC') && newValue != '') {
        var prefixedValue = 'MC' + newValue;
        g_form.setValue('fieldname', prefixedValue);
        g_form.setReadOnly('fieldname', true);
    }

 

By doing so your your avoiding regex.


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

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

   if (!newValue.startsWith('MC') && newValue != '') {
        var prefixedValue = 'MC' + newValue;
        g_form.setValue('basetype_name', prefixedValue);
        //g_form.setReadOnly('basetype_name', true);
    }
   
} does this script suppose to change the value dynamically on the form?

Community Alums
Not applicable

Hi @dev_K ,

If you want to get the error msg immediately after filling value on form , you can go with below onChange client script

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

if (!newValue.startsWith('MC')) {

g_form.setValue(control.name, oldValue); // Reset the field value to the old value
alert('Please enter a value with "MC" prefix.'); 
}
}



Else, if you want error message to display at the time of form submission, you can use this onSubmit client script -

function onSubmit() {
var fieldValue = g_form.getValue('your_variable_name'); // Replace 'your_variable_name' with the actual name of your variable

if (!fieldValue.startsWith('MC')) { 
g_form.addErrorMessage('Please enter a value with "MC" prefix.'); 
return false;
}

return true; 
}

Please mark as Accepted Solution if this solves your query .

Robbie
Kilo Patron
Kilo Patron

Hi @dev_K,

 

I've checked and confirmed the Question Regex Expression on my PDI and it works as expected on a sample Single Line Test field.

A question I do have is, have you implemented any other g_form.showfFeldMsg() method or check on that same field? How are you displaying the field message 'Value should start with the "MC" prefix'?

 

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