document.addEventListener('DOMContentLoaded', function () {
const phoneInput = document.querySelector('input[name="phone"], input[type="tel"]');
if (!phoneInput) return;
function isValidInternationalNumber(value) {
return /^+d{6,15}$/.test(value.trim());
}
function showError(message) {
let errorDiv = phoneInput.nextElementSibling;
if (!errorDiv || !errorDiv.classList.contains('phone-error')) {
errorDiv = document.createElement('div');
errorDiv.className = 'phone-error';
errorDiv.style.color = 'red';
errorDiv.style.fontSize = '0.9em';
phoneInput.parentNode.insertBefore(errorDiv, phoneInput.nextSibling);
}
errorDiv.textContent = message;
}
function clearError() {
const errorDiv = phoneInput.nextElementSibling;
if (errorDiv && errorDiv.classList.contains('phone-error')) {
errorDiv.textContent = '';
}
}
phoneInput.addEventListener('blur', function () {
if (!isValidInternationalNumber(phoneInput.value)) {
showError("Please enter your number with international prefix, e.g. +61412345678");
} else {
clearError();
}
});
const form = phoneInput.closest('form');
if (form) {
form.addEventListener('submit', function (e) {
if (!isValidInternationalNumber(phoneInput.value)) {
e.preventDefault();
showError("Phone number must start with '+' and country code.");
phoneInput.focus();
}
});
}
});