// 参考URL
// https://www.htmlhifive.com/conts/web/view/library/jquery-validate
// https://kudakurage.hatenadiary.com/entry/20091211/1260521031
$(function () {
var opt = {
rules: {
name: {required: true, maxlength: 100, }, tel: {required: true, tel: true, maxlength: 13, }, email: {required: true, email: true, }, todoufuken: {required: true, }, age: {required: true, digits: true, min: 1, max: 100, }, comment: {required: true, }, },
messages: {
name: {required: '必須項目です', maxlength: '入力内容が長すぎます', }, tel: {required: '必須項目です', tel: '電話番号が正しくありません', maxlength: '入力内容が長すぎます', }, email: {required: '必須項目です', email: 'メールアドレスの形式が正しくありません', }, todoufuken: {required: '必須項目です', }, age: {required: '必須項目です', digits: '年齢が正しくありません', min: '年齢が正しくありません', max: '年齢が正しくありません', }, comment: {required: '必須項目です', }, },
groups: {
},
// 挙動関連
onkeyup: false,
onfocusout: function onfocusout(element) {
this.element(element);
},
// エラーメッセージ設定
errorElement: "label", // エラーメッセージのタグ
errorClass: "error", // エラーメッセージにつけるclass
// wrapper: 'p', // エラーメッセージを囲むタグ
// エラーをまとめて表示の場合
// errorLabelContainer: $('#errors'), // 表示位置
// wrapper: "li", // エラーメッセージを囲むタグ
// エラー表示位置
errorPlacement: function (error, element) {
// デフォルト
error.appendTo(element.parents("div").children(".warning"));
},
// エラー箇所にスクロールさせる
invalidHandler: function (form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
$("html, body").animate({ scrollTop: $(validator.errorList[0].element.parentNode).offset().top - 150 });
}
},
// // 成功メッセージを表示させる
// success: function success(label, element) {
// $(label).addClass("success").text("OK");
// },
// highlight: function highlight(element, errorClass) {
// $(element).parents("div").find("label").removeClass("success");
// },
};
$("form").validate(opt);
});
// カスタムバリデータ
jQuery.validator.addMethod("katakana", function (value, element) {
return this.optional(element) || /^([ァ-ヶー ]+)$/.test(value);
}, "全角カタカナで入力してください");
jQuery.validator.addMethod("hiragana", function (value, element) {
return this.optional(element) || /^([ぁ-んー ]+)$/.test(value);
}, "全角ひらがなで入力してください");
jQuery.validator.addMethod("katahira", function (value, element) {
return this.optional(element) || /^([ぁ-んァ-ヶー ]+)$/.test(value);
}, "全角ひらがな・カタカナで入力してください");
jQuery.validator.addMethod("tel", function (value, element) {
return this.optional(element) || /^([0-9\-]+)$/.test(value);
}, "半角数字で入力して下さい");
jQuery.validator.addMethod("select", function (value, element, origin) {
return null != value && origin != value;
}, "選択してください");