How to hard code a url and then enter the suffix to that url, only hypen & under scores are allowed

vahini
Giga Guru

Just wanted to share this here so if anyone working on similar requirement can leverage it. 

 

I got a catalog requirement to create a variable on the catalog form to enter  URL and it should always start with a url then followed by a suffix to that url, this prefix url is always fixed, it wont change as shown below ( see image below)

 

https://theabc.sharepoint.com/sites/YourURL

 

in above url  " https://theabc.sharepoint.com/sites/   " is always fixed, user should only enter the url they wanted by appending it to this fixed url like https://theabc.sharepoint.com/sites/create share point sit.....

 

and this YourURL part should allow user to enter alpha numeric and Under scores and hypens and not any other special characters are allowed. 

 

 

 

vahini_0-1739304350173.png.

vahini_2-1739305297202.png

 

 

 

To achieve this i used an onchange client script + regex code

 

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue != '') {

        //var regex = /^https:\/\/theabc.sharepoint.com\/sites\/.*$/;
        var regex = /^https:\/\/thehartford.sharepoint.com\/sites\/(?:[A-Za-z0-9_-]+)*$/;
        if (!regex.test(newValue)) {
            g_form.clearValue('provide_the_custom_url');
            g_form.showFieldMsg('provide_the_custom_url', "Please follow the url format in the help text, enter the url you wanted into 'YourURL' space,only Hypens and under scores are allowed.", "error");
        }
    }
}

 

Line 4 in below code can be used if customer wants to allow any characters

 

vahini_1-1739305058872.png

 

Output : 

Test1 :

vahini_6-1739305675308.png

in above case didn't enter in correct format https://theabc.sharepoint.com/sites/YourURL so it thrown error msg to check help text and follow it.

 

vahini_5-1739305587517.png

 

 

Test2 :

in this case entered correct format, and in Your URL space entered the url i wanted using hypens and under scroes, no error displayed.

vahini_7-1739305831658.png

 

 

 

 

 

 

 

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@vahini 

try this

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue != '') {
        var regex = /^https:\/\/theabc\.sharepoint\.com\/sites\/[A-Za-z0-9_-]+$/;
        if (!regex.test(newValue)) {
            g_form.clearValue('provide_the_custom_url');
            g_form.showFieldMsg('provide_the_custom_url', "Please follow the URL format in the help text. Enter the URL you want into the 'YourURL' space. Only hyphens and underscores are allowed.", "error");
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@vahini 

it worked fine for me and allowed underscore and hyphen, It didn't allow @ or any other special character

function onChange(control, oldValue, newValue, isLoading) {

	g_form.hideFieldMsg('provide_the_custom_url');


    if (newValue != '') {
        var regex = /^https:\/\/theabc\.sharepoint\.com\/sites\/[A-Za-z0-9_-]+$/;
        if (!regex.test(newValue)) {
            g_form.clearValue('provide_the_custom_url');
            g_form.showFieldMsg('provide_the_custom_url', "Please follow the URL format in the help text. Enter the URL you want into the 'YourURL' space. Only hyphens and underscores are allowed.", "error");
        }
    }
}

Output:

url validation.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader