Skip to content

Commit df86ed9

Browse files
Utility: Validate the email address in the catalog whenever user is going to add another email
This is a client script that validates the email address in the MRVS field. It uses MutationObserver to observe the changes in the MRVS field. UseCase: Validate the email address in the catalog whenever user is going to add another email, and show an error message if a duplicate email address is found.
1 parent 09c2b13 commit df86ed9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* This is a client script that validates the email address in the MRVS field.
3+
* It uses MutationObserver to observe the changes in the MRVS field.
4+
* UseCase: Validate the email address in the catalog whenever user is going to add another email,
5+
* and show an error message if a duplicate email address is found.
6+
*/
7+
function onLoad() {
8+
var document = document || top.document;
9+
10+
var duplicateErrorMessageStatus = false; //adding this as corner case as observer will be called multiple times
11+
12+
setTimeout(function () {
13+
var tbody = document.querySelector("#user_details > div > tbody");
14+
15+
if (tbody) {
16+
var observer = new MutationObserver(function (m, o) {
17+
18+
var users = g_form.getValue('user_details');
19+
20+
var hasDuplicateEmails = validateUserDetails();
21+
22+
if (!users || hasDuplicateEmails) {
23+
if (hasDuplicateEmails && !duplicateErrorMessageStatus) {
24+
g_form.addErrorMessage('Duplicate email address found');
25+
duplicateErrorMessageStatus = true;
26+
}
27+
//disable the submit button
28+
} else {
29+
duplicateErrorMessageStatus = false;
30+
g_form.clearMessages();
31+
//enable the submit button
32+
}
33+
34+
});
35+
observer.observe(tbody, {
36+
attributes: true,
37+
childList: true,
38+
subtree: true
39+
});
40+
}
41+
}, 3000);
42+
43+
}
44+
45+
// MRVS contains the user details in the form of JSON
46+
function validateUserDetails() {
47+
var userDetailsMRVS = g_form.getValue('user_details');
48+
if (userDetailsMRVS) {
49+
var multiRowData = JSON.parse(userDetailsMRVS);
50+
var emailSet = new Set();
51+
52+
for (var i = 0; i < multiRowData.length; i++) {
53+
var row = multiRowData[i];
54+
55+
var email = row.email.trim().toLowerCase();
56+
57+
if (emailSet.has(email)) {
58+
59+
return true;
60+
}
61+
emailSet.add(email);
62+
}
63+
}
64+
return false;
65+
}

0 commit comments

Comments
 (0)