Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to provide validation for input URL field , either using string field or URL type field

Abhradipa Baner
Kilo Contributor

I had added an URL field in form where users will give URL values as input ; and I want to validate that URL field that means if user enters URL consisting (https / www) it will take the value OR ELSE if user enters string or integer value in place of URL field ,it will show alert message to the USER.

How can I fix this? 

Please help.

Regards,

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script + Regex for this

OR

you can also use onSubmit client script + Regex

Example onSubmit script

function onSubmit() {
	var url = g_form.getValue('url_field');
	var validateURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(url);

	if (!validateURL) {
		g_form.addErrorMessage(getMessage("Enter a valid URL"));
		return false;
	}
}

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Matt102
Giga Guru

Hi,

Add an onChange Client script to the field.

https://docs.servicenow.com/bundle/sandiego-application-development/page/script/client-scripts/concept/client-scripts.html

In that script use a regex to match what you will allow and display a message if format incorrect.

script _could_ be something like:

var re = <Your regex>;
if (!re.test(newValue)) {
  alert("invalid url");
    return false;
}

hth,matt

Hi again,

I always think its work looking at anything that ships  OOB.

I was just looking around client scripts and found on for the ldap_server_url table, it's called 'Validate Server URL for XSS' - not sure if you'll have that table. If not script is: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if (newValue.indexOf(">") > -1 || newValue.indexOf("<") > -1) {
		var msg = getMessage("Characters < and > are not allowed");
		g_form.showFieldMsg(control, msg, "error");
	} else {
		g_form.hideFieldMsg(control);
	}
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script + Regex for this

OR

you can also use onSubmit client script + Regex

Example onSubmit script

function onSubmit() {
	var url = g_form.getValue('url_field');
	var validateURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(url);

	if (!validateURL) {
		g_form.addErrorMessage(getMessage("Enter a valid URL"));
		return false;
	}
}

Regards
Ankur

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

Hi Ankur,

This is working. Thanks for the suggestion.

Regards,

Abhradipa