-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
134 lines (113 loc) · 4.19 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Inputs validation before submitting the form to Adobe Sign API.
// set up event listeners for the first email input field
var emailGroup = document.getElementsByClassName("emailGroup")[0];
addEmailOnBlurListener(emailGroup);
addDeleteOnClickListener(emailGroup);
// set up event listeners to the file input field
var file = document.getElementById("uploadFile");
file.addEventListener("change", function(event) {
var filePath = file.value;
var fileName = "";
if (filePath !== "") {
fileName = filePath.split('\\').pop().split('/').pop();
document.getElementById("fileLabel").innerHTML = fileName;
} else {
document.getElementById("fileLabel").innerHTML = "Select a Document";
}
disableButtons(undefined);
});
// validate if the selected file is a pdf file
function isValidPDF() {
var filePath = file.value;
var fileName = "";
var errMsg = document.getElementsByClassName("invalidFile")[0];
if (filePath !== "") {
fileName = filePath.split('\\').pop().split('/').pop();
var fileNameSplit = fileName.split(".");
var fileExt = fileNameSplit[fileNameSplit.length - 1];
if (fileExt.toLowerCase() !== "pdf") {
errMsg.style.display = "inline";
return false;
}
errMsg.style.display = "none";
return true;
}
errMsg.style.display = "none";
return false;
}
// add email input field event listner
function addEmailOnBlurListener(emailGroup){
var emailNode = emailGroup.getElementsByClassName("emailAddress")[0];
emailNode.addEventListener("blur", function(event) {
validateAllEmails(event.target);
});
}
// add delete email input field event listener
function addDeleteOnClickListener(emailGroup) {
var addOnNode = emailGroup.getElementsByClassName("deleteRecipient")[0];
addOnNode.addEventListener("click", function(event) {
if (document.getElementsByClassName("emailGroup").length > 1) {
emailGroup.parentNode.removeChild(emailGroup);
validateAllEmails(document.getElementsByClassName("emailAddress")[0]);
}
});
}
// add new email input fields
function addRecipient() {
disableButtons(true);
var emailGroups = document.getElementsByClassName("emailGroup");
var parent = emailGroups[0].parentElement;
var newEmailGroup = emailGroups[0].cloneNode(true);
newEmailGroup.getElementsByTagName("input")[0].value = "";
addEmailOnBlurListener(newEmailGroup);
addDeleteOnClickListener(newEmailGroup);
parent.append(newEmailGroup);
}
// validate single email address
function validateEmail(email) {
if (email !== "") {
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
}
return true;
}
// validate if all emails are entered and valid
function validateAllEmails(emailNode) {
var isDisabled = false;
var email = emailNode.value;
if (email === "") {
disableButtons(true);
} else if (!validateEmail(email)) {
emailNode.parentNode.nextElementSibling.style.display = "inline";
disableButtons(true);
} else {
emailNode.parentNode.nextElementSibling.style.display = "none";
var emailNodes = document.getElementsByClassName("emailAddress");
for (var i = 0; i < emailNodes.length; i++) {
var email = emailNodes[i].value;
if (!email || !validateEmail(email)) {
emailNodes[i].parentNode.nextElementSibling.style.display = "inline";
isDisabled = true;
}
}
disableButtons(isDisabled);
}
}
// disable add recipient button and/or submit button
function disableButtons(isDisabled) {
var addButton = document.getElementById("addRecipientButton");
var submitButton = document.getElementById("submitButton");
if (isDisabled !== undefined) {
addButton.disabled = isDisabled;
if (isDisabled) {
submitButton.disabled = isDisabled;
} else {
submitButton.disabled = !isValidPDF();
}
} else {
if (!isValidPDF()) {
submitButton.disabled = true;
} else if (isValidPDF() && !addButton.disabled) {
submitButton.disabled = false;
}
}
}