-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_list.js
148 lines (95 loc) · 3.19 KB
/
contact_list.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
let id = 1;
let contactListArr = []
let contactList = {
"name":"",
"contacts" : [],
addContact : function addContact(){
let contact = {};
var name = document.getElementById('name').value
// set contact obj by user value
contact.id = id;
contact.name = substring();
contact.mail = validateEmail();
contact.phone = checkPhoneAvailability();
// push contact to contacts array
if(contact.name != false || contact.mail != false || contact.phone != false ){
this.contacts.push(contact);
console.log(contactList);
id++;
}
else
{
alert("enter a valid data ");
}
},
removeContact : function removeContact( id ){},
editContact : function editContact (id, updateObj){},
getContact : function getContact (id){},
getAllContacts : function getAllContacts (){
console.log(this.contacts);
return this.contacts
},
}
var btn = document.getElementById('addBtn');
btn.onclick = function(){
substring()
//checkPhoneAvailability();
contactList.addContact();
}
var getContact = document.getElementById('getContact');
getContact.onclick = function(){
contactList.getAllContacts();
}
function validateEmail()
{
var mail = document.getElementById('mail').value
//var re = /\S+@\S+/;
var re = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
if ( re.test(mail) == true )
return mail
else
alert("contact mail is not valid");
return false
}
function checkPhoneAvailability(){
let phone2 = document.getElementById('phone')
if (phone2.value.length != 11 ){
alert("the phone number should be 11 number")
return false
}
else
return phone2.value
}
function substring(){
let name2 = document.getElementById('name').value
let first = name2[0];
var lastName = ""
var fulName = ""
for (let index = 0; index < name2.length; index++) {
if( name2[index] == " " ){
lastName =name2.substr(index , name2.length);
fulName = first+""+"."+lastName;
}
}
console.log("full name : " + fulName);
return fulName;
}
// by eng ziad
// function ContactList(contactName,contact ){
// this.contact = contact;
// this.contactName = contactName;
// }
// by eng ziad
// function Contact (name, mail , phone){
// this.name = name;
// this.mail = mail;
// this.phone = phone;
// this.id;
// }
// by eng ziad
// let myConctact = new Contact(name, mail, phone);
// myConctact.id = id;
// let myContactList = new ContactList(name, myConctact);
// contactListArr.push(myContactList);
// console.log(contactListArr);
// id++;