-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekthirdjs.js
297 lines (249 loc) · 10.3 KB
/
weekthirdjs.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//var xmlhttp = new XMLHttpRequest();
//
//xmlhttp.onreadystatechange = function () {
// if (xmlhttp.readyState == XMLHttpRequest.DONE) {
// if (xmlhttp.status == 200) {
// //document.getElementById("divid").innerHTML = xmlhttp.response[0].name;
// //alert(JSON.parse(xmlhttp.responseText));
// // console.log(xmlhttp.response[0].name);
// //var abc= JSON.parse(xmlhttp.response);
// //alert(abc);
// //console.log(xmlhttp.response);
// // json = xmlhttp.response;
// localStorage.setItem('testObject', JSON.stringify(xmlhttp.response));
// // console.log(json);
//// for(var i = 0; i < json.length; i++) {
////
//// var obj = json[i];
////
//// //console.log(obj.name+obj.location+obj.phone+obj.address);
////
////}
//
// } else if (xmlhttp.status == 400) {
// alert('There was an error 400');
// } else {
// alert('something else other than 200 was returned');
// }
// }
//};
//
//xmlhttp.open("GET", "weekthirdjson.json");
//xmlhttp.responseType = 'json';
//
//
//xmlhttp.send();
//var retrievedObject = localStorage.getItem('testObject');
//
//console.log(retrievedObject);
//var originalObject = JSON.parse(retrievedObject);
function populateInTable()
{
var table = document.getElementById('myTable');
while(table.rows.length > 1) {
table.deleteRow(1);
}
var retrievedObject = localStorage.getItem('testObject');
var originalObject = JSON.parse(retrievedObject);
for (var i in originalObject)
{
var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
// Insert a row in the table at row index 0
var newRow = tableRef.insertRow(tableRef.rows.length);
// Insert a cell in the row at index 0
// Append a text node to the cell
var newCell = newRow.insertCell(0);
var newText = document.createTextNode(originalObject[i].name);
newCell.appendChild(newText);
newCell = newRow.insertCell(1);
newText = document.createTextNode(originalObject[i].location);
newCell.appendChild(newText);
newCell = newRow.insertCell(2);
newText = document.createTextNode(originalObject[i].phone);
newCell.appendChild(newText);
newCell = newRow.insertCell(3);
newText = document.createTextNode(originalObject[i].address);
newCell.appendChild(newText);
newCell = newRow.insertCell(4);
newText = document.createTextNode(originalObject[i].email);
newCell.appendChild(newText);
}
}
function validateForm(event) {
event.preventDefault();
var lights = document.getElementsByClassName("has-error");
while (lights.length)
lights[0].className = lights[0].className.replace(/\bhas-error\b/g, "");
var elements = document.getElementsByClassName('errormsg');
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
var name = document.forms["myForm"]["fname"].value;
var location = document.forms["myForm"]["flocation"].value;
var phone = document.forms["myForm"]["fphone"].value;
var address = document.forms["myForm"]["faddress"].value;
var email = document.forms["myForm"]["femail"].value;
var nameSpan = document.createElement('span');
var locationSpan = document.createElement('span');
var phoneSpan = document.createElement('span');
var addressSpan = document.createElement('span');
var emailSpan = document.createElement('span');
nameSpan.setAttribute('class', 'errormsg text-danger');
locationSpan.setAttribute('class', 'errormsg text-danger');
phoneSpan.setAttribute('class', 'errormsg text-danger');
addressSpan.setAttribute('class', 'errormsg text-danger');
emailSpan.setAttribute('class', 'errormsg text-danger');
var validation_success = true;
if (name == "") {
document.forms["myForm"].children[0].appendChild(nameSpan);
nameSpan.innerHTML = 'Name must be filled out!';
document.forms["myForm"].children[0].className += " has-error";
validation_success = false;
} else if (name.length < 2)
{
document.forms["myForm"].children[0].appendChild(nameSpan);
nameSpan.innerHTML = 'The length of Name should be atleast 2 characters!';
validation_success = false;
document.forms["myForm"].children[0].className += " has-error";
}
if (location == "") {
document.forms["myForm"].children[1].appendChild(locationSpan);
locationSpan.innerHTML = 'location must be filled out!';
validation_success = false;
document.forms["myForm"].children[1].className += " has-error";
} else if (location.length < 2)
{
document.forms["myForm"].children[1].appendChild(locationSpan);
locationSpan.innerHTML = 'The length of Location should be atleast 2 characters!';
validation_success = false;
document.forms["myForm"].children[1].className += " has-error";
}
if (phone == "") {
document.forms["myForm"].children[2].appendChild(phoneSpan);
phoneSpan.innerHTML = 'phone must be filled out!';
validation_success = false;
document.forms["myForm"].children[2].className += " has-error";
} else if (phone.length < 2)
{
document.forms["myForm"].children[2].appendChild(phoneSpan);
phoneSpan.innerHTML = 'The length of phone should be atleast 2 characters!';
validation_success = false;
document.forms["myForm"].children[2].className += " has-error";
} else if (isNaN(phone))
{
document.forms["myForm"].children[2].appendChild(phoneSpan);
phoneSpan.innerHTML = 'The phone should be Numbers only!';
validation_success = false;
document.forms["myForm"].children[2].className += " has-error";
}
if (address == "") {
document.forms["myForm"].children[3].appendChild(addressSpan);
addressSpan.innerHTML = 'address must be filled out!';
validation_success = false;
document.forms["myForm"].children[3].className += " has-error";
} else if (address.length < 2)
{
document.forms["myForm"].children[3].appendChild(addressSpan);
addressSpan.innerHTML = 'The length of address should be atleast 2 characters!';
validation_success = false;
document.forms["myForm"].children[3].className += " has-error";
}
if (email == "") {
document.forms["myForm"].children[4].appendChild(emailSpan);
emailSpan.innerHTML = 'email must be filled out!';
validation_success = false;
document.forms["myForm"].children[4].className += " has-error";
} else if (email.length < 2)
{
document.forms["myForm"].children[4].appendChild(emailSpan);
emailSpan.innerHTML = 'The length of email should be atleast 2 characters!';
validation_success = false;
document.forms["myForm"].children[4].className += " has-error";
} else
{
var emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (!email.match(emailformat))
{
document.forms["myForm"].children[4].appendChild(emailSpan);
emailSpan.innerHTML = 'The email should be in proper format!';
validation_success = false;
document.forms["myForm"].children[4].className += " has-error";
}
}
if (validation_success)
{
var retrievedObject = localStorage.getItem('testObject');
//console.log(retrievedObject);
var originalObject = JSON.parse(retrievedObject);
originalObject.push({"name":name,"location":location,"phone":phone, "address":address,"email":email});
localStorage.setItem('testObject', JSON.stringify(originalObject));
console.log(localStorage.getItem('testObject'));
populateInTable();
// var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
//
// // Insert a row in the table at row index 0
// var newRow = tableRef.insertRow(tableRef.rows.length);
//
// // Insert a cell in the row at index 0
//
// // Append a text node to the cell
// var newCell = newRow.insertCell(0);
// var newText = document.createTextNode(name);
// newCell.appendChild(newText);
//
// newCell = newRow.insertCell(1);
// newText = document.createTextNode(location);
// newCell.appendChild(newText);
//
// newCell = newRow.insertCell(2);
// newText = document.createTextNode(phone);
// newCell.appendChild(newText);
//
// newCell = newRow.insertCell(3);
// newText = document.createTextNode(address);
// newCell.appendChild(newText);
//
// newCell = newRow.insertCell(4);
// newText = document.createTextNode(email);
// newCell.appendChild(newText);
document.forms["myForm"].reset();
}
return false;
}
var getStudentJSONData = function () {
var promise = new Promise(function (resolve, reject) {
setTimeout(function () {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
resolve(xmlhttp.response);
} else if (xmlhttp.status == 400) {
alert('There was an error 400');
} else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "weekthirdjson.json");
xmlhttp.responseType = 'json';
xmlhttp.send();
}, 2000);
});
return promise;
};
var fn_return = getStudentJSONData();
fn_return.then(function (success_response) {
var retrievedObject = localStorage.getItem('testObject');
if(!retrievedObject)
localStorage.setItem('testObject', JSON.stringify(success_response));
populateInTable();
},
function (err) {
document.write(err);
});