-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (40 loc) · 1.48 KB
/
script.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
// Function to validate the form fields
function validateForm() {
if (document.getElementById('email').value === '') {
alert('Email is required');
return false;
}
if (document.getElementById('password').value === '') {
alert('Password is required');
return false;
}
if (document.getElementById('firstName').value === '') {
alert('First Name is required');
return false;
}
if (document.getElementById('lastName').value === '') {
alert('Last Name is required');
return false;
}
return true;
}
// Function to sanitize user input to prevent XSS attacks
function sanitizeInput(input) {
return input.replace(/<script.*?>.*?<\/script>/gi, 'sanitized');
}
// Add event listener to the form for the submit event
document.getElementById('userForm').addEventListener('submit', function(event) {
event.preventDefault();
// Perform form validation
if (!validateForm()) {
return; // Stop the form submission if validation fails
}
// Sanitize the comments input field to prevent XSS
let comments = document.getElementById('comments').value;
let sanitizedComments = sanitizeInput(comments);
console.log('Sanitized comments:', sanitizedComments);
// Display submission message
let submissionMessage = document.getElementById('submission-message');
submissionMessage.textContent = 'Data Submitted!';
submissionMessage.style.display = 'block'; // Show the message
});