How to validate the variable with validation regex?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 09:25 AM - edited 01-04-2024 09:26 AM
Hi,
I have a custom template variable which is created in u_m2m_template_variable table. I have it to have validation regex and based on the regex it should work otherwise should show some error mistake to enter a proper input based on the regex. I have created a validation regex field in the same table. But struggling in linking the logic of template variable 'u_display_name' with 'u_validation_regex' .How to link it so that the variable should accept only inputs based on the regex?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2024 02:56 AM
Here are the steps to link the logic of template variable 'u_display_name' with 'u_validation_regex':
1. Create a new client script or modify the existing one for the table where the template variable 'u_display_name' is used.
2. In the client script, use the 'onChange' or 'onBlur' event for the 'u_display_name' field.
3. In the script, get the value of the 'u_validation_regex' field from the 'u_m2m_template_variable' table.
4. Use the JavaScript 'test()' method to test the value of the 'u_display_name' field against the regex pattern.
5. If the test fails, use the 'g_form.addErrorMessage()' method to display an error message.
Here is a sample code:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Get the regex pattern
var gr = new GlideRecord('u_m2m_template_variable');
gr.get('u_display_name', g_form.getValue('u_display_name'));
var regexPattern = gr.getValue('u_validation_regex');
// Create a regex object
var regex = new RegExp(regexPattern);
// Test the value of the 'u_display_name' field
if (!regex.test(newValue)) {
// If the test fails, display an error message
g_form.addErrorMessage('Please enter a proper input based on the regex.');
}
}
Remember to replace 'u_display_name' and 'u_validation_regex' with the actual field names in your instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2024 05:36 AM
Hi @Buddy 1 ,
You can create a on change client script on the 'u_display_name' variable with below script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var rgx = g_form.getValue('u_validation_regex'); //get the regex from the field, you can use glideAjax call to get it from server side, if the field is not available on the form.
if (!rgx.test(newValue)){ // test the regex is matching with the new value, if not then show error msg
g_form.clearValue('u_display_name');
g_form.showFieldMsg('u_display_name', ' Invalid format. Value should be in the format : "' + regex + '"', 'error');
return false;
}
}
If my answer solves your issue, please mark it as Helpful 👍 and Accepted ✔️ based on impact.
Thanks,
Vrushali Kolte