- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2022 11:08 AM
Hi experts
I have a onSubmit client script that is failing .....
my task is to verify the email format being inputted.
So for some weird reason, the code works for evaluating 1 email, and it also works in Xplore.
However, when the user inputs more than one email, the code does not work and I get a weird error message
onSubmit script error: TypeError: emailToValidate.match is not a function:
function () { [native code] }
So this is how the code works,
check if there are multiple emails by looking for a comma in the field value "cc",
if there arent' evaluated the email using the function validateEmail function
if there are multiple emails, evaluate each email using validateEmail function
function onSubmit() {
function validateEmail(emailToValidate) {
var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
if (emailToValidate.match(validRegex) && emailToValidate.split('@')[1].indexOf('.') != -1) {
return true;
} else {
g_form.showFieldMsg('cc', 'Incorrect format');
return false;
}
}
var emailToValidate = g_form.getValue('cc');
var isEmailValid = true;
// verify if multiple emails present
if (emailToValidate.indexOf(',') != -1) {
// there are multiple emails present
var arrEmails = emailToValidate.split(',');
for (var i in arrEmails) {
isEmailValid = validateEmail(arrEmails[i]);
if (!isEmailValid) break;
}
} else {
isEmailValid = validateEmail(emailToValidate);
}
return isEmailValid;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2022 03:44 PM
this is what worked::
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
function validateEmail(emailToValidate) {
/*
Email validation rules:
emails cant have commas
email should only have 1 @
email should only have 1 . after @
*/
var isATPresent = emailToValidate.indexOf("@");
if (isATPresent == -1) return false;
var areMultipleATSPresent = emailToValidate.split("@").length != 2;
if (areMultipleATSPresent) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
var userName = emailToValidate.split("@")[0];
var mailServer = emailToValidate.split("@")[1];
var isPeriodPresent = mailServer.indexOf(".");
if (isPeriodPresent == -1) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
var areMultiplePeriodsPresent = mailServer.split(".").length != 2;
if (areMultiplePeriodsPresent) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
return true;
}
function validateEmailString(emailToValidate) {
// split multiple emails if more than 1 email is present
var areMultipleEmailsPresent = emailToValidate.includes(",");
if (areMultipleEmailsPresent) {
var arrEmails = emailToValidate.split(",");
for (var i =0 ; arrEmails.length > i ; i++) {
if (!validateEmail(arrEmails[i])) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
}
} else {
if (!validateEmail(emailToValidate)) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
}
return true;
}
var emailToValidate = newValue;
validateEmailString(emailToValidate);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2022 11:24 AM
use test function.
your_reg_ex.test(your_string)//returns true or false
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2022 01:18 PM
Hello,
It's not really necessary or appropriate to state in your question topic about "points easy", "medium points", etc. Your question topic itself is what helps drive responses and is important, not baiting in point chasers.
For some reason, you've developed a habit of doing this as it's in almost all of your question posts.
If it's easy, then in theory you could search and work on the result. It's a bit confusing how you're having the issue, but then gauging and telling the Community that it's "easy points".
Besides all that, it's recommended to review this article for assistance: https://community.servicenow.com/community?id=community_article&sys_id=243449861be230d017d162c4bd4bc...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2022 03:44 PM
this is what worked::
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
function validateEmail(emailToValidate) {
/*
Email validation rules:
emails cant have commas
email should only have 1 @
email should only have 1 . after @
*/
var isATPresent = emailToValidate.indexOf("@");
if (isATPresent == -1) return false;
var areMultipleATSPresent = emailToValidate.split("@").length != 2;
if (areMultipleATSPresent) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
var userName = emailToValidate.split("@")[0];
var mailServer = emailToValidate.split("@")[1];
var isPeriodPresent = mailServer.indexOf(".");
if (isPeriodPresent == -1) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
var areMultiplePeriodsPresent = mailServer.split(".").length != 2;
if (areMultiplePeriodsPresent) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
return true;
}
function validateEmailString(emailToValidate) {
// split multiple emails if more than 1 email is present
var areMultipleEmailsPresent = emailToValidate.includes(",");
if (areMultipleEmailsPresent) {
var arrEmails = emailToValidate.split(",");
for (var i =0 ; arrEmails.length > i ; i++) {
if (!validateEmail(arrEmails[i])) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
}
} else {
if (!validateEmail(emailToValidate)) {
g_form.showErrorBox("cc", "Incorrect format");
return false;
}
}
return true;
}
var emailToValidate = newValue;
validateEmailString(emailToValidate);
}