The CreatorCon Call for Content is officially open! Get started here.

email and URL validation

deepika46
Tera Contributor

Hello experts,

 

Consider 2 fields,

 

a=xyz@yahoo.com

b=https://DE-QA-001.servicenow/asdefdd

 

I have to validate these 2 email and URL values in seriver side script 

 

Can anyone please help me. I am new to ServiceNow scripting

 

2 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @deepika46 

We can achieve it by defining these fields under Email and URL type.

email.png

url.png

 

And also, we can create our own validation.

  1. Define your own Regex (you can also refer to OOTB Regex for Email and URL)
  2. Create your OnSubmit Client Script. Sample below

 

 

var regex = /^(?![\.])[a-zA-Z0-9!#$%*\/?|^{}`~&'\+\-=_.]+(?<![.]+)@(?![\.-])[a-zA-Z0-9-_\.]+(\.[a-zA-Z0-9_]+)$/;
if(!regex.test(newValue)){
	g_form.showErrorBox('<field_name>', 'Email is not valid');
	return false;
}

 

 

 

Reference OOTB Regular Expression:

URL: https://<instance_name>/question_regex.do?sys_id=264d3729876b1300e0ef0cf888cb0b47

Email: https://<instance_name>/question_regex.do?sys_id=e005e481530020107d13ddeeff7b12e5

 

Let me know if it works for you

 

Cheers,

Tai Vu

View solution in original post

Hi @deepika46 ,

 

Certainly! You can validate the email and URL values in ServiceNow using server-side scripting. Below are the steps and examples for validating an email address and a URL within a ServiceNow server-side script.

 

### Validating Email Address:

 

To validate an email address, you can use a regular expression pattern to match the email format. Here's an example of how you can validate the `a` field as an email address:

 

 

(function() {

    var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    var a = 'xyz@yahoo.com'; // Replace this with your actual field value



    if (emailPattern.test(a)) {

        gs.info('Valid email address: ' + a);

    } else {

        gs.info('Invalid email address: ' + a);

    }

})();

 

In this example, the `emailPattern` regular expression is used to validate the `a` field as an email address. If it matches the email format, it's considered valid.

 

### Validating URL:

 

To validate a URL, you can use a regular expression pattern to match the URL format. Here's an example of how you can validate the `b` field as a URL:

 

(function() {

    var urlPattern = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;

    var b = 'https://DE-QA-001.servicenow/asdefdd'; // Replace this with your actual field value



    if (urlPattern.test(b)) {

        gs.info('Valid URL: ' + b);

    } else {

        gs.info('Invalid URL: ' + b);

    }

})();

In this example, the `urlPattern` regular expression is used to validate the `b` field as a URL. If it matches the URL format, it's considered valid.

 

Please note that these regular expressions are basic and may not cover all edge cases. Adjust the regular expressions according to your specific validation requirements. These scripts can be executed in a background script in ServiceNow to validate the provided email and URL values.

 

Mark my answer helpful & accepted if it helps you resolve your issue

 

Thanks,

Danish

View solution in original post

4 REPLIES 4

Danish Bhairag2
Tera Sage

Hi @deepika46 ,

 

Validate exactly what? Could u please elaborate.

 

Is it whether the values are email & URL only ?

 

Thanks,

Danish

Yes. Like in email field if someone enters hello,, it should coz an error

Hi @deepika46 ,

 

Certainly! You can validate the email and URL values in ServiceNow using server-side scripting. Below are the steps and examples for validating an email address and a URL within a ServiceNow server-side script.

 

### Validating Email Address:

 

To validate an email address, you can use a regular expression pattern to match the email format. Here's an example of how you can validate the `a` field as an email address:

 

 

(function() {

    var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    var a = 'xyz@yahoo.com'; // Replace this with your actual field value



    if (emailPattern.test(a)) {

        gs.info('Valid email address: ' + a);

    } else {

        gs.info('Invalid email address: ' + a);

    }

})();

 

In this example, the `emailPattern` regular expression is used to validate the `a` field as an email address. If it matches the email format, it's considered valid.

 

### Validating URL:

 

To validate a URL, you can use a regular expression pattern to match the URL format. Here's an example of how you can validate the `b` field as a URL:

 

(function() {

    var urlPattern = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;

    var b = 'https://DE-QA-001.servicenow/asdefdd'; // Replace this with your actual field value



    if (urlPattern.test(b)) {

        gs.info('Valid URL: ' + b);

    } else {

        gs.info('Invalid URL: ' + b);

    }

})();

In this example, the `urlPattern` regular expression is used to validate the `b` field as a URL. If it matches the URL format, it's considered valid.

 

Please note that these regular expressions are basic and may not cover all edge cases. Adjust the regular expressions according to your specific validation requirements. These scripts can be executed in a background script in ServiceNow to validate the provided email and URL values.

 

Mark my answer helpful & accepted if it helps you resolve your issue

 

Thanks,

Danish

Tai Vu
Kilo Patron
Kilo Patron

Hi @deepika46 

We can achieve it by defining these fields under Email and URL type.

email.png

url.png

 

And also, we can create our own validation.

  1. Define your own Regex (you can also refer to OOTB Regex for Email and URL)
  2. Create your OnSubmit Client Script. Sample below

 

 

var regex = /^(?![\.])[a-zA-Z0-9!#$%*\/?|^{}`~&'\+\-=_.]+(?<![.]+)@(?![\.-])[a-zA-Z0-9-_\.]+(\.[a-zA-Z0-9_]+)$/;
if(!regex.test(newValue)){
	g_form.showErrorBox('<field_name>', 'Email is not valid');
	return false;
}

 

 

 

Reference OOTB Regular Expression:

URL: https://<instance_name>/question_regex.do?sys_id=264d3729876b1300e0ef0cf888cb0b47

Email: https://<instance_name>/question_regex.do?sys_id=e005e481530020107d13ddeeff7b12e5

 

Let me know if it works for you

 

Cheers,

Tai Vu