forked from kabdelrazek-do/SecurityAndAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform.html
More file actions
150 lines (140 loc) · 5.32 KB
/
webform.html
File metadata and controls
150 lines (140 loc) · 5.32 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SafeVault - Secure Data Management</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.form-container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #333;
}
input[type="text"], input[type="email"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box;
}
input[type="text"]:focus, input[type="email"]:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 5px rgba(76, 175, 80, 0.3);
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
}
button:hover {
background-color: #45a049;
}
.error {
color: #f44336;
font-size: 14px;
margin-top: 5px;
}
.success {
color: #4CAF50;
font-size: 14px;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="form-container">
<h1>SafeVault - User Registration</h1>
<p>Secure data management with input validation and protection against common vulnerabilities.</p>
<form id="userForm" action="/api/users" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<div id="username-error" class="error"></div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<div id="email-error" class="error"></div>
</div>
<button type="submit">Submit</button>
<div id="form-message"></div>
</form>
</div>
<script>
// Client-side input validation
document.getElementById('userForm').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('username').value;
const email = document.getElementById('email').value;
// Clear previous errors
document.getElementById('username-error').textContent = '';
document.getElementById('email-error').textContent = '';
document.getElementById('form-message').textContent = '';
let isValid = true;
// Validate username
if (!username || username.length < 3) {
document.getElementById('username-error').textContent = 'Username must be at least 3 characters long';
isValid = false;
} else if (!/^[a-zA-Z0-9_]+$/.test(username)) {
document.getElementById('username-error').textContent = 'Username can only contain letters, numbers, and underscores';
isValid = false;
}
// Validate email
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!email || !emailRegex.test(email)) {
document.getElementById('email-error').textContent = 'Please enter a valid email address';
isValid = false;
}
if (isValid) {
// Submit to server
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, email })
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('form-message').textContent = 'User registered successfully!';
document.getElementById('form-message').className = 'success';
document.getElementById('userForm').reset();
} else {
document.getElementById('form-message').textContent = data.message || 'Registration failed';
document.getElementById('form-message').className = 'error';
}
})
.catch(error => {
document.getElementById('form-message').textContent = 'An error occurred. Please try again.';
document.getElementById('form-message').className = 'error';
});
}
});
</script>
</body>
</html>