- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2016 12:49 PM
I am currently looking at a Record Producer I created that needs to have many different fields that are inputting different emails. There are many articles that address the issue of email validation within a field, but I wanted to not have to modify each script individually if something needed changing. I came up with a number of options.
- Create Catalog Client Scripts for onLoad/onChange
- Too many fields to update and maintain. Makes me feel like less for not having a central function to rule them all.
- Use oob Service Now email validation
- Using the actual Email field Type, which does check the field automagically.
- This field type is hidden? Why?
- Should I override and un-hide email fields type?
- Reasons against or for?
- Using the actual Email field Type, which does check the field automagically.
- Another is using the built in function discussed in this article Validate Email field
- Can't get the code to work below in my Catalog Client Script:
- function onChange(control, oldValue, newValue, isLoading) {
- if (isLoading || newValue == '') {
- return;
- }
- //Email Validation Using OOB
- if(oldValue != newValue){
- g_form.hideFieldMsg('email', true);
- if (!isEmailValid(newValue)) {
- g_form.showFieldMsg('email', newValue + " is an invalid email, please re-enter email in correct format.",'error');
- }
- }
- Am I missing something on how to properly call this function?
- Can't get the code to work below in my Catalog Client Script:
- Create my own global Include/function
- Haven't dug into this one yet!
Thanks in Advance!
Jason M Fisher
University of Kentucky
ITSM Developer
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 05:09 AM
Well, I found some interesting stuff...
On line 9527 thru 9567:
Here's the actual code:
function isEmailValid(value) {
var problemMsg = isEmailValidWithReason(value);
if (problemMsg != "") {
jslog("isEmailValid: " + problemMsg);
return false;
}
return true;
}
function isEmailValidWithReason(value) {
var localPartChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%*/?|^{}`~&'+-=_.";
var domainChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
if (value.indexOf("@") == -1)
return "missing @ sign";
var s = value.split("@");
if (s.length != 2)
return "too many at signs";
if (!containsOnlyChars(localPartChars, s[0]))
return "invalid character before the at sign";
if (s[0].length < 1)
return "at least one character must be before the at sign";
if (s[0].substr(0, 1) == ".")
return "period cannot be the first character";
if (s[0].substr(s[0].length - 1, 1) == ".")
return "period cannot be the last character before the at sign";
if (!containsOnlyChars(domainChars, s[1]))
return "invalid character after the at sign";
var periodIndex = s[1].indexOf(".");
if (periodIndex == -1)
return "missing period after the at sign";
if (periodIndex == 0)
return "period cannot be the first character after the at sign";
var periods = s[1].split(".");
var lastPeriod = periods[periods.length - 1];
if (lastPeriod.length < 1)
return "must be at least 1 character after the last period";
if (!isAlphaNum(s[1].substr(0, 1)))
return "the first character after the at sign must be alphanumeric";
if (!isAlphaNum(s[1].substr(s[1].length - 1, 1)))
return "the last character must be alphanumeric";
return "";
}
Now, you can take that code and rename it and stuff it into your own Client Script or Script Include, or whatever you'd like.
Oh, they reference a function in there called containsOnlyChars() which can be found on lines 9855 thru 9864:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 05:47 AM
So why don't you use the actual code behind isEmailValid() into your own scoped function as I've depicted below?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 06:04 AM
New to this, so I was ensuring I wasn't missing something simple where I don't need to recreate the function and then have to potentially maintain more code later.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 08:38 AM
OK, I WAS missing something! I stepped back to HELLO WORLD alerts using isEmailValid() and it worked fine! That took me down looking at how the g_form.hideErrorBox() function worked.
From Scripting Alert, Info, and Error Messages - ServiceNow Wiki:
- g_form.showErrorBox("field_name", "Hello World"); Will put "Hello World" in an error message below the specified field.
- g_form.hideErrorBox("field_name"); Will hide an error box that is visible under the specified field.
Where my script was using generic 'Email' field name, this needs to be changed to match each of the field names where it is used.
So simple and I feel not so bright for not catching it sooner!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 08:47 AM
Well, the research for an alternative certainly taught me a few things. I'm glad for your post and continued discussion. The whole journey made us all a bit wiser and more connected as a community!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 09:30 AM
OK, so once I got rid of that stop point, it didn't take any effort to come up with this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Email Validation Using OOB
if(oldValue != newValue){
g_form.hideFieldMsg('u_owner_email_1', true);
var problemMsg = isEmailValidWithReason(newValue);
if (problemMsg != "") {
g_form.showFieldMsg('u_owner_email_1', newValue + " is an invalid email, " + problemMsg + ".");
}
}
}
I just recreated isEmailValid().
I will turn this into another piece of validation script so that I don't interfere with any OOB issues isEmailValid() is connected with already.