How to allow only email type in string field and restrict other string types?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:41 AM
HI,
we have created a string field where user should enter the text in email format and it should not allow any other text type. please provide any solution.
Thanks,
Sirisha.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:55 AM
Hi Sirisha,
You will need to write an onchange client script on your field or variable which would be something like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (!newValue.match(mailformat)) {
alert('You have entered an invalid email address!');
}
}
Please mark my answer helpful/correct if this works for you.
Regards,
Vismit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 03:24 AM
Hi,
you can use RegEx for this
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
03-31-2022 03:53 AM
Hi
Try this code I have tested code in my PDI.
Change the field name as per your requirement.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var Reformat = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
if (!Reformat.test(newValue)) {
g_form.clearValue("description");
g_form.showFieldMsg("description",'Please enter valid email address!',"error");
}
}
Mark as Correct answer and Helpful if applicable.
Reagrds,
Imran Ahmad.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 04:17 AM
If this is for Service Catalog, you can use OOB email validation.
OOB Email validation rule can be seen below:
/nav_to.do?uri=question_regex.do?sys_id=e005e481530020107d13ddeeff7b12e5
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022