RegEx on catalog item variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 01:21 AM
Hi!
I am trying to make sure that users are submitting values that have "MC" prefix eg. MCtest123.
Here is the regex I created:
here, in the variable details I have added 'MC' to automatically populate the field:
however when i type sth here Im getting an error:
and next when i remove what i have added before, same error reappears:
and the error message disappears when I remove the entire line including "MC"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 02:01 AM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 02:46 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 09:49 PM
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 .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 03:22 AM
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