- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2021 07:15 AM
Hi Team,
How to write catalog client script to give email validation as it can accept only @(companyname).com users only.
Field is a single line text field(email Id).
Thanks
Solved! Go to Solution.
- Labels:
-
Multiple Versions
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2021 10:25 PM
Not sure but that option should be there for single line text variable
If not then try to set it from List view of the variable if you don't wish to add to form layout
OR
you can create onChange client script on that variable and sample script below
UI Type - ALL
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
g_form.hideErrorBox('variable');
var regex = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;
newValue = newValue.toString();
var isValidFormat = regex.test(newValue);
if(!isValidFormat){
g_form.showErrorBox('variable','Please enter valid email address','error');
g_form.clearValue('variable');
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 11:52 PM
The fully RFC 822 compliant regex is inefficient and obscure for validate email address because of its length. Fortunately, RFC 822 was superseded twice and the current specification for email addresses is RFC 5322. RFC 5322 leads to a regex that can be understood if studied for a few minutes and is efficient enough for actual use.
If you use HTML5, use this code:
<input type="email" name="email" required placeholder="Enter a valid email address">
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 09:27 AM
There is a ServiceNow created onSubmit Client Script that comes with the "Human Resources: Core" application called Validate Email onSubmit.
Here is the script:
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideErrorBox('email');
return validateEmail('email');
}
function validateEmail(field) {
var email = g_form.getValue(field);
if (!email || email.trim().length <= 0)
return true;
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!re.test(email)) {
g_form.showErrorBox(field, getMessage('Invalid Email'));
return false;
}
return true;
}
