Skip to content

Utility: Validate the email address in the catalog whenever user is going to add another email #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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.
*/
function onLoad() {
var document = document || top.document;

var duplicateErrorMessageStatus = false; //adding this as corner case as observer will be called multiple times

setTimeout(function () {
var tbody = document.querySelector("#user_details > div > tbody");

if (tbody) {
var observer = new MutationObserver(function (m, o) {

var users = g_form.getValue('user_details');

var hasDuplicateEmails = validateUserDetails();

if (!users || hasDuplicateEmails) {
if (hasDuplicateEmails && !duplicateErrorMessageStatus) {
g_form.addErrorMessage('Duplicate email address found');
duplicateErrorMessageStatus = true;
}
//disable the submit button
} else {
duplicateErrorMessageStatus = false;
g_form.clearMessages();
//enable the submit button
}

});
observer.observe(tbody, {
attributes: true,
childList: true,
subtree: true
});
}
}, 3000);

}

// MRVS contains the user details in the form of JSON
function validateUserDetails() {
var userDetailsMRVS = g_form.getValue('user_details');
if (userDetailsMRVS) {
var multiRowData = JSON.parse(userDetailsMRVS);
var emailSet = new Set();

for (var i = 0; i < multiRowData.length; i++) {
var row = multiRowData[i];

var email = row.email.trim().toLowerCase();

if (emailSet.has(email)) {

return true;
}
emailSet.add(email);
}
}
return false;
}
Loading