File size: 2,771 Bytes
727a40a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function togglePasswordVisibility() {
    const passwordField = document.getElementById('password');
    const toggleIcon = document.getElementById('toggle-password');

    if (passwordField.type === 'password') {
        passwordField.type = 'text';
        toggleIcon.innerHTML = '<img src="https://pdf813.netlify.app/chat/img/icon/icons8-closed-eye-50.png" alt="Hide Password" />';

    } else {
        passwordField.type = 'password';
        toggleIcon.innerHTML = '<img src="https://pdf813.netlify.app/chat/img/icon/icons8-eye-48.png" alt="Show Password" />';
    }

}

// Form validation and error handling
const form = document.getElementById('login-form');
const emailInput = document.getElementById('email');
const passwordInput = document.getElementById('password');
const emailError = document.getElementById('email-error');
const passwordError = document.getElementById('password-error');
const loginBtn = document.getElementById('login-btn');

form.addEventListener('submit', function (event) {
    event.preventDefault();
    let hasError = false;

    // Clear previous error messages
    emailError.textContent = '';
    passwordError.textContent = '';

    // Validate email
    if (emailInput.value.trim() === '') {
        emailError.textContent = 'Please fill in the email field';
        hasError = true;
    } else if (!validateEmail(emailInput.value)) {
        emailError.textContent = 'Email is not valid';
        hasError = true;
    }

    // Validate password
    if (passwordInput.value.trim() === '') {
        passwordError.textContent = 'Please fill in the password field';
        hasError = true;
    }

    if (hasError) return;

    // Show spinner and disable button
    loginBtn.disabled = true;
    loginBtn.innerHTML = 'Logging in... <div class="spinner" id="spinner"></div>';
    const spinner = document.getElementById('spinner');
    spinner.style.display = 'inline-block';

    // Simulate an API request for login (replace this with your actual login API call)
    setTimeout(function () {
        // Simulate a response: if login fails
        let loginSuccess = false; // Change this to simulate success/failure
        if (!loginSuccess) {
            emailError.textContent = 'Email not found or incorrect password';
            passwordError.textContent = 'Please try again';
            spinner.style.display = 'none';
            loginBtn.disabled = false;
            loginBtn.innerText = "Login";
        } else {
            window.location.href = '/dashboard'; // Redirect to dashboard after successful login
        }
    }, 3000); // Shortened timeout for testing purposes
});

function validateEmail(email) {
    const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
    return regex.test(email);
}