-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
128 lines (108 loc) · 3.69 KB
/
project.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
//create object to store inputed data
let userDetailsDatabase = {};
//to make sure user keeps getting prompted when an input is not valid (using loop)
function getUserDetail() {
//to set up an alert bar(prompt) requesting for username once a user reloads the page or clicks the button
let userName = prompt("Enter your username");
//to terminate filling of input if a user doesnt put in an input or if a user clicks on cancel without filling an input
if (userName == null) {
return;
}
//setting the requirements for the username(validating) using conditionals
function validateUserName(userName) {
if (userName.length < 10 && userName.length > 0) {
return true;
} else {
return false;
}
}
while (validateUserName(userName) == false) {
userName = prompt("Username must be less than 10 and greater than 0");
}
//to store the data(username) inputed by the user in the object we created
userDetailsDatabase["userName"] = userName;
//console.log(userDetailsDatabase);
//validating the username using console.log with the function
// console.log(validateUserName(userName));
//setting a prompt for email address
let email = prompt("Enter your email address");
if (email == null) {
return;
}
//validating the email address
function validateEmail(email) {
const emailCheck =
//assign standard code for checking email to the constant emailCheck
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/gi;
//test your validation to see if it works, returns true if email is valid and false if it isnt
emailCheckResult = emailCheck.test(email);
//setting conditionals that depend on the validation test result
if (emailCheckResult == true) {
return true;
} else {
return false;
}
}
while (validateEmail(email) == false) {
email = prompt("Enter a valid email");
}
userDetailsDatabase["email"] = email;
// console.log(validateEmail(email));
//setting a prompt for phone number
let phoneNumber = prompt("Enter your phone number");
if (phoneNumber == null) {
return;
}
//validating the phone number
function validatePhoneNumber(phoneNumber) {
if (phoneNumber.length == 11) {
return true;
} else {
return false;
}
}
while (validatePhoneNumber(phoneNumber) == false) {
phoneNumber = prompt("Phone number must be 11 digits, try again!");
}
userDetailsDatabase["phoneNumber"] = phoneNumber;
//console.log(validatePhoneNumber(phoneNumber));
//setting a prompt for password
let password = prompt("Enter your password");
if (password == null) {
return;
}
function validatePassword(password) {
if (password.length < 6) {
return false;
} else {
return true;
}
}
while (validatePassword(password) == false) {
password = prompt("Password must not be less than 6 digits");
}
//console.log(validatePassword(password));
let confirmPassword = prompt("Confirm your password");
if (confirmPassword == null) {
return;
}
function validateConfirmPassword(confirmPassword) {
if (confirmPassword != password) {
return false;
} else {
return true;
}
}
if (validateConfirmPassword(confirmPassword) == false) {
confirmPassword = prompt(
"Confirm password does not match password, try again"
);
}
// console.log(validateConfirmPassword(confirmPassword));
}
function displayUserDetails() {
alert(`Your Details\n\nUsername: ${userDetailsDatabase.userName}\n
Phone number: ${userDetailsDatabase.phoneNumber}\n
Email: ${userDetailsDatabase.email}`);
}
//dont call multiple functions in a function, call them outside the main function then invoke them in the main function.