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

Validate email field input

gaidem
ServiceNow Employee
ServiceNow Employee

I have a client script which will ensure input into an email field contains a valid email address:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//If the new value isn't blank
if(newValue != '') {
//Type appropriate comment here, and begin script below
var oldV = oldValue;
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
strEmail = g_form.getValue(email);

// search email text for regular exp matches
if (strEmail.search(validRegExp) == -1)
{
alert('A valid e-mail address is required.');
g_form.setValue('email',oldV);
}


}
}
}

14 REPLIES 14

CapaJC
ServiceNow Employee
ServiceNow Employee

I've gotta take some sort of class on regular expressions some day. This is cool.

Out of box, if a field has an internal_type of "email" (like the Email field on the sys_user form), the following Validation Script (found in System Definition) runs on form submit:



function validate(value) {
if (value == null || value == "")
return true;
if (isEmailValid(value))
return true;
else
return "Invalid email address";
}


The functions behind that are:


function isEmailValid(value){
var problemMsg = isEmailValidWithReason(value);
if (problemMsg != "") {
jslog("isEmailValid: " + problemMsg);
return false;
}
return true;
}

// validate email address and return a reason why it is not valid
function isEmailValidWithReason(value) {
var localPartChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%*/?|^{}`~&'+-=_.";
var domainChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";

// break into local part and domain
if (value.indexOf("@") == -1)
return "missing @ sign";
var s = value.split("@");
if (s.length != 2)
return "too many at signs";

// check the local part of the address
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";

// check the domain part of the address
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 < 2)
return "must be at least 2 characters 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 ""; // address is OK
}


I am not able to find this function isEmailValid(). can you please point me where this function is defined?

Thanks.


So that script is ServiceNows OOB validation for emails?



How are you able to get to that script to know what it is?


Where is the function isEmailValid() defined?   Where is the script that contains the definition for the isEmailValid() function that you have provided?



Thanks in advance.