jQuery(document).ready(function($) { const $countrySelect = $('#user_country'); const $stateSelect = $('#user_state'); const $stateOtherInput = $('#user_state_other'); const $registrationForm = $('#hvac-registration-form'); // Form validation helpers function showFieldError(fieldId, message) { const $field = $('#' + fieldId); const $existingError = $field.siblings('.error-message'); if ($existingError.length) { $existingError.text(message); } else { $field.after('
'); } $field.addClass('error'); } function clearFieldError(fieldId) { const $field = $('#' + fieldId); $field.siblings('.error-message').remove(); $field.removeClass('error'); } // Real-time email validation $('#user_email').on('blur', function() { const email = $(this).val(); if (email && !isValidEmail(email)) { showFieldError('user_email', 'Please enter a valid email address.'); } else { clearFieldError('user_email'); } }); $('#business_email').on('blur', function() { const email = $(this).val(); if (email && !isValidEmail(email)) { showFieldError('business_email', 'Please enter a valid business email address.'); } else { clearFieldError('business_email'); } }); function isValidEmail(email) { const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return re.test(email); } // Real-time password validation $('#user_pass').on('input blur', function() { const password = $(this).val(); if (password) { const errors = []; if (password.length < 8) { errors.push('at least 8 characters'); } if (!/[A-Z]/.test(password)) { errors.push('one uppercase letter'); } if (!/[a-z]/.test(password)) { errors.push('one lowercase letter'); } if (!/[0-9]/.test(password)) { errors.push('one number'); } if (errors.length > 0) { showFieldError('user_pass', 'Password must contain ' + errors.join(', ') + '.'); } else { clearFieldError('user_pass'); // Check confirm password if it has a value const confirmPass = $('#confirm_password').val(); if (confirmPass) { $('#confirm_password').trigger('blur'); } } } }); // Confirm password validation $('#confirm_password').on('blur', function() { const password = $('#user_pass').val(); const confirmPassword = $(this).val(); if (confirmPassword && password !== confirmPassword) { showFieldError('confirm_password', 'Passwords do not match.'); } else { clearFieldError('confirm_password'); } }); // URL validation for optional fields function isValidURL(url) { try { new URL(url); return true; } catch (_) { return false; } } $('#user_url, #user_linkedin, #business_website').on('blur', function() { const url = $(this).val(); const fieldId = $(this).attr('id'); if (url && !isValidURL(url)) { const fieldName = fieldId === 'user_url' ? 'personal website' : fieldId === 'user_linkedin' ? 'LinkedIn profile' : 'business website'; showFieldError(fieldId, 'Please enter a valid URL for your ' + fieldName + '.'); } else { clearFieldError(fieldId); } }); // Form submission validation $registrationForm.on('submit', function(e) { let hasErrors = false; const errors = []; // Check required fields const requiredFields = [ { id: 'user_email', name: 'Email' }, { id: 'user_pass', name: 'Password' }, { id: 'confirm_password', name: 'Confirm Password' }, { id: 'first_name', name: 'First Name' }, { id: 'last_name', name: 'Last Name' }, { id: 'display_name', name: 'Display Name' }, { id: 'description', name: 'Biographical Info' }, { id: 'business_name', name: 'Business Name' }, { id: 'business_phone', name: 'Business Phone' }, { id: 'business_email', name: 'Business Email' }, { id: 'business_description', name: 'Business Description' }, { id: 'user_country', name: 'Country' }, { id: 'user_city', name: 'City' }, { id: 'user_zip', name: 'Zip/Postal Code' }, { id: 'application_details', name: 'Application Details' } ]; requiredFields.forEach(field => { const $field = $('#' + field.id); if (!$field.val() || $field.val().trim() === '') { hasErrors = true; errors.push(field.name + ' is required.'); showFieldError(field.id, field.name + ' is required.'); } }); // Check state/province const country = $('#user_country').val(); if (country) { if (country === 'United States' || country === 'Canada') { const state = $('#user_state').val(); if (!state || state === '') { hasErrors = true; errors.push('State/Province is required.'); showFieldError('user_state', 'State/Province is required.'); } } else { const otherState = $('#user_state_other').val(); if (!otherState || otherState.trim() === '') { hasErrors = true; errors.push('State/Province is required.'); showFieldError('user_state_other', 'State/Province is required.'); } } } // Check radio buttons if (!$('input[name="create_venue"]:checked').length) { hasErrors = true; errors.push('Please select whether to create a training venue profile.'); } if (!$('input[name="business_type"]:checked').length) { hasErrors = true; errors.push('Business Type is required.'); } // Check checkbox groups const checkboxGroups = [ { name: 'training_audience[]', label: 'Training Audience' }, { name: 'training_formats[]', label: 'Training Formats' }, { name: 'training_locations[]', label: 'Training Locations' }, { name: 'training_resources[]', label: 'Training Resources' } ]; checkboxGroups.forEach(group => { if (!$('input[name="' + group.name + '"]:checked').length) { hasErrors = true; errors.push('Please select at least one option for ' + group.label + '.'); } }); if (hasErrors) { e.preventDefault(); // Show error summary at the top let $errorSummary = $('.hvac-form-errors'); if (!$errorSummary.length) { $errorSummary = $('