Allow only Hypens ande under scores in a field on catalog form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 12:49 PM
Posting this here if anyone working on similar requirement.
Create a field called 'Site Title' , it should accept only alpha numeric and two special characters Hypens (-) , under scores (_) , space and nothing else ( @,#,$ ..............nothing is allowed )
To achieve this used onchange client script + regex code
a-z allows lower case
A-Z allows upper case
0-9 allows numeric
\s allows spaces
- under score is allowed
- hyper is allowed
*$ means allow only these characters
function onChange(control, oldValue, newValue, isLoading) {
if (newValue != '') {
var regex = /^[a-zA-Z0-9\s_-]*$/;
if (!regex.test(newValue)) {
g_form.clearValue('site_title');
g_form.showFieldMsg('site_title', "Special characters allowed are Hypen and Under score only.", "error");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 02:03 PM
Since this is a variable on a Catalog Item, you can also achieve this using the Variable Validation Regex feature without a script. Define the validation via Variable Validation Regex in the left nav with whatever Validation message you want:
then select this Validation Regex on the Type Specifications tab of the variable definition.