var formName = "#mail_form";
// 確認画面の生成処理
function confirmGenerate(){
var Confirm = document.querySelector(formName).cloneNode(true);
$(Confirm).find("input[value='SEND MESSAGE']").remove();
$(Confirm).find("input[type='hidden']").remove();
$(Confirm).find("button[type=submit]").remove();
val = $(Confirm).find("[name=purpose]:checked").val();
$(Confirm).find("[name=purpose]").parent().parent().parent().removeClass("radio");
$(Confirm).find("[name=purpose]").parent().parent().parent().find(".radio").removeClass("radio");
$(Confirm).find("[name=purpose]").parent().parent().replaceWith('
' + val + '
');
$(Confirm).find("input, textarea").replaceWith(function() {
$(this).replaceWith('' + $(this).val() + '
')
});
return "以下の内容で送信します。
よろしいでしょうか?" + "";
}
$().ready(function() {
$.validator.setDefaults({
errorElement: "div",
submitHandler: function() {
var Confirm = confirmGenerate();
Swal.fire({
title: '送信前確認',
html : Confirm,
showCancelButton: true,
confirmButtonText: '送信',
cancelButtonText: '戻る',
reverseButtons: true,
width : '80%',
showClass: {
popup: 'animated faster',
// icon: 'animated heartBeat delay-1s'
},
hideClass: {
popup: 'animated fadeOutUp faster',
// icon: 'animated heartBeat delay-1s'
},
}).then((result) => {
// 送信ボタンの検知
if (result.isConfirmed) {
// googleReCaptchaStart
grecaptcha.enterprise.ready(function() {
grecaptcha.enterprise.execute('6LdYo3glAAAAAJZGDZneMlH7Z3vT6K6qPlqJ_obh', {action: 'submit'}).then(function(token){
// Add your logic to submit to your backend server here.
var mailForm = document.querySelector(formName);
var recaptchaToken = mailForm.querySelector('[name="recaptchaToken"]');
if(!recaptchaToken){
recaptchaToken = document.createElement("input");
recaptchaToken.setAttribute("name", "recaptchaToken");
recaptchaToken.setAttribute("type", "hidden");
mailForm.appendChild(recaptchaToken);
}
recaptchaToken.value = token;
// フォーム送信処理
$.ajax({
url : $(formName).attr('action'),
type: $(formName).attr('method'),
data: $(formName).serialize(),
})
.done(function(data) { // 送信成功時の処理
var result = JSON.parse(data)
if(result.result){
if(result.redirectURL){
location.href = result.redirectURL;
}else{
Swal.fire({
icon: 'success',
title: result.msg,
});
}
}else{
Swal.fire({
icon: 'error',
title: result.msg,
})
}
})
.fail(function() { // 送信失敗時の処理
Swal.fire({
icon: 'error',
title: '送信失敗しました。',
})
});
});
});
}
})
}
});
// validate the comment form when it is submitted
// validate signup form on keyup and submit
$(formName).validate({
rules : {
purpose : {
required : true
},
name : {
required : true
},
email : {
required : true,
email : true
},
tel : {
pattern : /(^0\d{1,3}-\d{1,4}-\d{4}$|^0\d{9,10}$)/i
}
},
messages : {
purpose : "お問い合わせ内容を選択してください。",
name : "お名前は必須です",
email : {
required : "メールアドレスは必須です。",
email : "メールアドレスが正しくありません。"
},
tel : {
pattern : "電話番号は形式が正しくありません。"
}
}
});
});