Allow only Hypens ande under scores in a field on catalog form

vahini
Giga Guru

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 ) 

 

vahini_0-1739306785861.png

 

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

vahini_1-1739306854611.png

 

 

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");
		}
    }
}

 

 

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

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:

BradBowman_1-1739311355946.png

then select this Validation Regex on the Type Specifications tab of the variable definition.

BradBowman_0-1739311304032.png