Fixes.
This commit is contained in:
@@ -171,13 +171,82 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
console.log('Enhanced animations initialized');
|
||||
|
||||
// Form Validation and Enhancement
|
||||
// Enhanced Form Validation with Anti-Spam Measures
|
||||
const contactForm = document.querySelector('.contact-form form');
|
||||
|
||||
if (contactForm) {
|
||||
// Track form interactions for bot detection
|
||||
let formInteractions = {
|
||||
mouseMovements: 0,
|
||||
keystrokes: 0,
|
||||
focusEvents: 0,
|
||||
startTime: Date.now(),
|
||||
fields: {}
|
||||
};
|
||||
|
||||
// Add hidden timestamp field
|
||||
const timestampField = document.createElement('input');
|
||||
timestampField.type = 'hidden';
|
||||
timestampField.name = 'form_timestamp';
|
||||
timestampField.value = Date.now();
|
||||
contactForm.appendChild(timestampField);
|
||||
|
||||
// Add hidden interaction token
|
||||
const interactionToken = document.createElement('input');
|
||||
interactionToken.type = 'hidden';
|
||||
interactionToken.name = 'interaction_token';
|
||||
contactForm.appendChild(interactionToken);
|
||||
|
||||
// Track mouse movements (humans move mouse)
|
||||
let mouseMoveHandler = function() {
|
||||
if (formInteractions.mouseMovements < 100) {
|
||||
formInteractions.mouseMovements++;
|
||||
}
|
||||
};
|
||||
document.addEventListener('mousemove', mouseMoveHandler);
|
||||
|
||||
// Track interactions on form fields
|
||||
contactForm.querySelectorAll('input, textarea, select').forEach(field => {
|
||||
field.addEventListener('keydown', function() {
|
||||
formInteractions.keystrokes++;
|
||||
if (!formInteractions.fields[field.name]) {
|
||||
formInteractions.fields[field.name] = {
|
||||
keystrokes: 0,
|
||||
changes: 0,
|
||||
focusTime: 0
|
||||
};
|
||||
}
|
||||
formInteractions.fields[field.name].keystrokes++;
|
||||
});
|
||||
|
||||
field.addEventListener('focus', function() {
|
||||
formInteractions.focusEvents++;
|
||||
if (formInteractions.fields[field.name]) {
|
||||
formInteractions.fields[field.name].focusTime = Date.now();
|
||||
}
|
||||
});
|
||||
|
||||
field.addEventListener('change', function() {
|
||||
if (formInteractions.fields[field.name]) {
|
||||
formInteractions.fields[field.name].changes++;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
contactForm.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
// Calculate interaction score
|
||||
const timeSpent = Date.now() - formInteractions.startTime;
|
||||
const interactionScore = calculateInteractionScore(formInteractions, timeSpent);
|
||||
|
||||
// Set interaction token
|
||||
interactionToken.value = btoa(JSON.stringify({
|
||||
score: interactionScore,
|
||||
time: timeSpent,
|
||||
interactions: formInteractions.mouseMovements + formInteractions.keystrokes + formInteractions.focusEvents
|
||||
}));
|
||||
|
||||
// Basic form validation
|
||||
const formData = new FormData(this);
|
||||
const name = formData.get('name');
|
||||
@@ -203,6 +272,12 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
// Check interaction score (low score = likely bot)
|
||||
if (interactionScore < 30) {
|
||||
errors.push('Please complete the form normally');
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
if (isValid) {
|
||||
// Show loading state
|
||||
const submitButton = this.querySelector('button[type="submit"]');
|
||||
@@ -210,18 +285,29 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
submitButton.textContent = 'Sending...';
|
||||
submitButton.disabled = true;
|
||||
|
||||
// Submit form (you'll need to implement the backend handler)
|
||||
// Submit form with XMLHttpRequest header
|
||||
fetch('contact-handler.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
body: formData,
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
showNotification('Message sent successfully! We\'ll get back to you soon.', 'success');
|
||||
showNotification(data.message || 'Message sent successfully! We\'ll get back to you soon.', 'success');
|
||||
this.reset();
|
||||
// Reset interaction tracking
|
||||
formInteractions = {
|
||||
mouseMovements: 0,
|
||||
keystrokes: 0,
|
||||
focusEvents: 0,
|
||||
startTime: Date.now(),
|
||||
fields: {}
|
||||
};
|
||||
} else {
|
||||
showNotification('There was an error sending your message. Please try again.', 'error');
|
||||
showNotification(data.message || 'There was an error sending your message. Please try again.', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -238,6 +324,29 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate interaction score to detect bots
|
||||
function calculateInteractionScore(interactions, timeSpent) {
|
||||
let score = 0;
|
||||
|
||||
// Time-based scoring (bots submit too fast)
|
||||
if (timeSpent > 5000) score += 20; // More than 5 seconds
|
||||
if (timeSpent > 10000) score += 20; // More than 10 seconds
|
||||
if (timeSpent > 30000) score += 10; // More than 30 seconds
|
||||
|
||||
// Mouse movement scoring (humans move mouse)
|
||||
if (interactions.mouseMovements > 10) score += 20;
|
||||
if (interactions.mouseMovements > 50) score += 10;
|
||||
|
||||
// Keystroke scoring (humans type)
|
||||
if (interactions.keystrokes > 20) score += 20;
|
||||
if (interactions.keystrokes > 50) score += 10;
|
||||
|
||||
// Focus event scoring (humans tab/click between fields)
|
||||
if (interactions.focusEvents > 3) score += 10;
|
||||
|
||||
return Math.min(score, 100); // Cap at 100
|
||||
}
|
||||
|
||||
// Email validation function
|
||||
function isValidEmail(email) {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
Reference in New Issue
Block a user