function validateEmail(email) { const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return re.test(email); } document.getElementById('changeEmailForm')?.addEventListener('submit', async function(e) { e.preventDefault(); const currentPassword = document.getElementById('currentPasswordEmail').value; const currentEmail = document.getElementById('currentEmail').value; const newEmail = document.getElementById('newEmail').value; // Validation if (!currentPassword) { showNotification('Please enter your current password', 'error'); return; } if (!newEmail) { showNotification('Please enter a new email address', 'error'); return; } if (!validateEmail(newEmail)) { showNotification('Please enter a valid email address', 'error'); return; } if (newEmail === currentEmail) { showNotification('New email cannot be the same as current email', 'error'); return; } try { const response = await fetch('/api/change_email.php', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': '' // Add CSRF protection }, body: JSON.stringify({ currentPassword: currentPassword, newEmail: newEmail }) }); const result = await response.json(); if (result.success) { showNotification('Email updated successfully! A verification link has been sent to your new email.', 'success'); document.getElementById('currentEmail').value = newEmail; this.reset(); } else { showNotification(result.message || 'Email change failed', 'error'); } } catch (error) { console.error('Error:', error); showNotification('Network error - please try again', 'error'); } });