-
Notifications
You must be signed in to change notification settings - Fork 0
/
myscript.js
231 lines (197 loc) · 7.7 KB
/
myscript.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Variable is declared to distinguish between odd and even row elemnts
let count = 0;
// document.querySelector("button").addEventListener("click",addInfo)
// function for displaying the information in the table
function addInfo()
{
var nameCheck = nameValid();
//if name is not valid return and display error message
if(nameCheck == false){
return;
}
//if Email is not valid return and display error message
var emailCheck = emailValid();
if (emailCheck==false){
return;
}
//if Website is not valid return and display error message
var websiteCheck = websiteValid();
if(websiteCheck == false){
return;
}
//if gender is not choosen display a error message
var genderCheck = genderValid();
if(genderCheck==false){
return;
}
//choose atleast one skill, if not display error messg
var SkillCheck = SkillValid();
if(SkillCheck == false){
return;
}
//Confirmation before submitting the form
var conf = confirm("Do you want to submit");
if(conf == false){
return
}
var genderOutput = genderChoice();
var skillOutput = skillChoices();
if ($("#productTable tbody").length == 0) {
$("#productTable").append("<tbody></tbody>");
}
// Odd row elements have different styling than even row
if (count % 2 == 0) {
$("#productTable tbody").append(
"<tr>" +
"<td id='newData' class='animated fadeIn' style='height:100px'>" +
"<b>" + $("#name").val() + "</b>" + "<br>" +
genderOutput + "<br>" +
$("#email").val() + "<br>" +
'<a href=" '+ "https://" + $("#website").val() +' " target="_blank" >' + $("#website").val() +'</a>' + "<br>" +
skillOutput +
"</td>" +
"<td id='newData' class='animated fadeIn'>" +
'<a href="' + $("#imageLink").val() + '" target="_blank">' + '<img src="'+ $("#imageLink").val() + '" alt="Photo" title="Click to open in new tab" style="width:125px;height:100px"></a>' +
"</td>" +
"</tr>");
}
// Even Row elements
else {
$("#productTable tbody").append(
"<tr>" +
"<td id='newData' class='animated fadeIn' style='height:100px'>" +
"<b>" + $("#name").val() + "</b>" + "<br>" +
genderOutput + "<br>" +
$("#email").val() +"<br>" +
'<a href="' + "https://" + $("#website").val() + '" target="_blank" >' + $("#website").val()+'</a>' + "<br>" +
skillOutput +
"</td>" +
"<td id='newData' class='animated fadeIn'>" + '<a href="' + $("#imageLink").val() + '" target="_blank">' + '<img src="' + $("#imageLink").val() + '" alt="Photo" title="Click to open in new tab" style="width:125px;height:100px"></a>' +
"</td>" +
"</tr>");
}
count += 1;
document.getElementById("myForm").reset()
}
// Gives the checked radio key of gender
function genderChoice()
{
const choices = document.querySelectorAll('input[name="genderChoice"]');
let selectedValue;
for (const choice of choices) {
if (choice.checked) {
selectedValue = choice.value;
break;
}
}
return selectedValue; // already in string
}
// Gives the skill choices made by user
function skillChoices()
{
const choices = document.querySelectorAll('input[name="skillChoices"]');
//console.log(choices);
let selectedValue = [];
for (let choice in choices) {
//console.log(choices[choice].value);
if (choices[choice].checked) {
selectedValue.push(choices[choice].value);
}
}
return selectedValue.toString(); //in array to string
}
// Validation of Email
function emailValid()
{
var text = document.getElementById("email").value;
var regex = /^([a-z0-9\.-]+)@([a-z0-9-]+)\.([a-z]{2,8})(\.[a-z]{2,8})?$/ ;
if(regex.test(text)){
document.getElementById('emailLabel').style.visibility="hidden" ;
document.getElementById('errorMsg').style.visibility="hidden" ;
// console.log("true");
return true;
}
else if(text.length==0){
document.getElementById('emailLabel').innerHTML='Enter Email';
document.getElementById('emailLabel').style.visibility="visible";
document.getElementById('errorMsg').style.visibility="visible" ;
alert("Please Enter your email to Proceed");
}
else{
document.getElementById('emailLabel').innerHTML='Invalid Email';
document.getElementById('emailLabel').style.visibility="visible";
alert("Enter a valid email id ! Eg: virat@gmail.com");
// text.style.border="solid 3px red";
// document.getElementById('email').style.border="solid 3px red";
}
return false;
}
// Validation of Name
function nameValid(){
var nametext = document.getElementById('name').value;
if(nametext.length==0){
document.getElementById('nameLabel').style.visibility="visible";
document.getElementById('errorMsg').style.visibility="visible" ;
alert("Please enter your name to proceed");
}
else{
document.getElementById('nameLabel').style.visibility="hidden" ;
document.getElementById('errorMsg').style.visibility="hidden" ;
return true;
}
return false
}
// Validation of Website
function websiteValid()
{
var websiteText = document.getElementById('website').value;
var WebRegex = /^(https?:\/\/)?(www\.)?[0-9a-z\.]+\.[-_0-9a-z]+[A-Z0-9a-z/\.-]+$/;
if(WebRegex.test(websiteText)){
document.getElementById('websiteLabel').style.visibility="hidden" ;
document.getElementById('errorMsg').style.visibility="hidden";
return true;
}
else if(websiteText.length==0){
alert('Enter a URL to proceed !');
document.getElementById('websiteLabel').innerHTML="Enter URL";
document.getElementById('websiteLabel').style.visibility="visible";
document.getElementById('errorMsg').style.visibility="visible" ;
}
else{
document.getElementById('websiteLabel').innerHTML="Invalid URL";
document.getElementById('websiteLabel').style.visibility="visible";
alert("Enter a Valid URL ! Eg: \"http://www.google.com\" ");
}
return false
}
// Validation of gender
function genderValid()
{
gendercheckMale = document.getElementById('male');
genderCheckFemale = document.getElementById('female');
if(gendercheckMale.checked || genderCheckFemale.checked){
document.getElementById('errorMsg').style.visibility="hidden" ;
return true;
}
else{
document.getElementById('errorMsg').style.visibility="visible" ;
alert("Enter your Gender to proceed");
return false;
}
}
// Validation of Skill
function SkillValid()
{
skillCheckJava = document.getElementById('java');
skillCheckHtml = document.getElementById('html');
skillCheckCss = document.getElementById('css');
if(skillCheckJava.checked || skillCheckHtml.checked || skillCheckCss.checked ){
document.getElementById('errorMsg').style.visibility="hidden" ;
return true;
}
else{
document.getElementById('errorMsg').style.visibility="visible" ;
alert("Enter atleast one skill to Enroll !");
return false;
}
}