const API_ENDPOINT = 'https://240q3u70wg.execute-api.ap-northeast-1.amazonaws.com/v1/contact-product-form'; const API_KEY = 'F2x5rMYRGx1VLfhItnxie7s89BU3Fniv1WqEJPDJ'; //---------------------------------- // main //---------------------------------- document.addEventListener('DOMContentLoaded', function () { const form = document.getElementById('mailformpro'); form.addEventListener('submit', async function (e) { e.preventDefault(); // バリデーション const hasError = valideteData(form); if (hasError) { const errorMessage = document.getElementById('error-message'); errorMessage.style.display = 'block'; const rect = errorMessage.getBoundingClientRect(); const absoluteTop = rect.top + window.scrollY; window.scrollTo({ top: absoluteTop - 200, behavior: 'smooth' }); return; } // データをAPIに送信 //ガスター様AWS環境以外は、ポストしないで、そのまま完了画面を出す。 await postDataToApi(form); //window.location.href = 'thanks.html'; }); }); //---------------------------------- // valideteData //---------------------------------- function valideteData(form) { let hasError = false; const fields = { 'kind' : 'お問い合わせの製品・サービスは必須です', 'name' : 'お名前は必須です', 'kana' : 'ふりがなは必須です', 'company' : '会社名は必須です', 'email' : 'メールアドレスは必須です', // 'tel' : '電話番号は必須です', 'contact_method' : 'ご希望の連絡方法は必須です', 'inquiry' : 'お問い合わせ内容は必須です', 'policy' : 'プライバシーポリシーに同意が必要です', }; Object.entries(fields).forEach(([field, message]) => { let value = form.elements[field].value; // メールアドレス if (field === 'email' && value) { const emailConfirm = form.elements['email_confirm'].value; const emailRegex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; if (!emailRegex.test(value)) { document.getElementById(`${field}-error`).innerText = '有効なメールアドレスを入力してください'; hasError = true; return; } else if (value !== emailConfirm) { document.getElementById(`${field}-error`).innerText = '確認用メールアドレスとメールアドレスが一致しません'; hasError = true; return; } } //問い合わせ内容(500文字以内) if (field === 'inquiry' && value) { if (form.elements['inquiry'].value.length >= 500) { document.getElementById(`${field}-error`).innerText = '問い合わせ内容は500文字以内で入力してください('+ form.elements['inquiry'].value.length + '文字入力)'; hasError = true; return; } } // プライバシーポリシー if (field === 'policy') { value = !!document.querySelector(`input[name="${field}"]:checked`); } if (!value) { document.getElementById(`${field}-error`).innerText = message; hasError = true; } else { document.getElementById(`${field}-error`).innerText = ''; } }); return hasError; } //---------------------------------- // postDataToApi //---------------------------------- async function postDataToApi(form) { const formData = new FormData(form); const object = {}; formData.forEach((value, key) => { object[key] = value; }); try { const response = await fetch(API_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': API_KEY, }, body: JSON.stringify(object), }); if (response.status === 200) { window.location.href = 'thanks.html'; } else { console.error('送信失敗'); } } catch (error) { console.error('エラーが発生しました: ' + error.toString()); } } //----------------------------------