- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 12:03 PM - edited 09-05-2024 12:15 PM
I created a onchange client script to prevent special characters from being entered on the field but my field message is not displaying. can anyone help. is this not applicable or is there n issue with the code. i
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Disallow special characters in abbreviation name
var alpha = /^[\s0-9a-zA-Z]*$/;
//
var isValid = alpha.test(newValue);
if (!isValid) {
// Show an error message
g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');
// Clear the invalid input
g_form.setValue('field_name', oldValue);
} else {
// Remove any previous error message if the input is valid
g_form.hideFieldMsg('field_name');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 12:17 PM
Try a more traditional regex approach
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Disallow special characters in abbreviation name
var alpha = new RegExp("^[\s0-9a-zA-Z]*$");
//
var isValid = alpha.test(newValue);
if (!isValid) {
// Show an error message
g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');
// Clear the invalid input
g_form.setValue('field_name', oldValue);
} else {
// Remove any previous error message if the input is valid
g_form.hideFieldMsg('field_name');
}
}
And if you are doing this on a Catalog Item variable as opposed to a form/table field, try the variable validation feature instead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 12:17 PM
Try a more traditional regex approach
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Disallow special characters in abbreviation name
var alpha = new RegExp("^[\s0-9a-zA-Z]*$");
//
var isValid = alpha.test(newValue);
if (!isValid) {
// Show an error message
g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');
// Clear the invalid input
g_form.setValue('field_name', oldValue);
} else {
// Remove any previous error message if the input is valid
g_form.hideFieldMsg('field_name');
}
}
And if you are doing this on a Catalog Item variable as opposed to a form/table field, try the variable validation feature instead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 01:05 PM
i looked up the variable validation feature and did it that way. I wasn't privy to this addition. Thankyou
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2025 10:14 PM
Hi DevtoSME,
In above code you need to just 'set the value' before the Field Error Message is shown.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Disallow special characters in abbreviation name
var alpha = /^[\s0-9a-zA-Z]*$/;
//
var isValid = alpha.test(newValue);
if (!isValid) {
// Clear the invalid input
g_form.setValue('field_name', oldValue);
// Show an error message
g_form.showFieldMsg('field_name', 'Special characters are not allowed.', 'error');
} else {
// Remove any previous error message if the input is valid
g_form.hideFieldMsg('field_name');
}
}