- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 12:04 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 10:58 PM - edited 10-09-2023 12:26 AM
Hi @deepika46
We can achieve it by defining these fields under Email and URL type.
And also, we can create our own validation.
- Define your own Regex (you can also refer to OOTB Regex for Email and URL)
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 11:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 12:18 PM - edited 10-08-2023 12:20 PM
Hi @deepika46 ,
Validate exactly what? Could u please elaborate.
Is it whether the values are email & URL only ?
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 01:07 PM
Yes. Like in email field if someone enters hello,, it should coz an error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 11:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2023 10:58 PM - edited 10-09-2023 12:26 AM
Hi @deepika46
We can achieve it by defining these fields under Email and URL type.
And also, we can create our own validation.
- Define your own Regex (you can also refer to OOTB Regex for Email and URL)
- 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