0
次のスクリプトを使用して、一部のフォームでjqueryの検証をカスタマイズしています。このスクリプトは一度呼び出され、テンプレートファイルによっていくつかの異なるフォームの1つを生成するために使用されます。つまり、スクリプトはすべてのフォームに対して呼び出されます。要素が存在しないときにjqueryが失敗する
すべてのフォームにタイトル要素があるため、他の2つの検証(有効期限と開始日/終了日)のみが適用されるため、一番上の部分(//タイトルがあることを確認してください)は、いずれかのフォームには、そうでなければ、それらのフィールドは存在しません。
"(index):1966 Uncaught TypeError:これらの要素を持たないフォームを送信しようとすると、プロパティ 'replace' of undefinedを読み取ることができません。最初に要素を追加します。また、検証jqueryはこれが発生したときに表示されません。
私は間違っていますか?
jQuery(document).ready(function($){
//Make the title required
acf.add_filter('validation_complete', function(json, $form){
//Make sure there is a title
if(!$("#_post_title").val()) {
var temp = new Object();
temp["input"] = "_post_title";
temp["message"] = "A Title is required";
json.errors.push(temp);
}
//Make sure the expiration is 3 months or less away (auditions)
if ($("#acf-field_574257a8eb3f0")!== null) {
var expRaw = $("input#acf-field_574257a8eb3f0").val();
var expDate = expRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')
var expiration = Date.parse(expDate);
var max = (3).months().fromNow();
if (expiration > max) {
var temp = new Object();
temp["input"] = "acf[field_574257a8eb3f0]";
temp["message"] = "Maximum of 3 months from today!";
json.errors.push(temp);
}
}
//Make sure start date is before end date and that start date is in the future
if ($("#acf-field_5701b4d1d11d0") !== null) {
var startRaw = $("input#acf-field_5701b4d1d11d0").val();
var endRaw = $("input#acf-field_5701b4ecd11d1").val();
var startDate = startRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')
var endDate = endRaw.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')
var start = Date.parse(startDate);
var end = Date.parse(endDate);
if (start < Date.parse("now")) {
var temp = new Object();
temp["input"] = "acf[field_5701b4d1d11d0]";//start date
temp["message"] = "Start date must be in the future.";
json.errors.push(temp);
} else if (start > end){
var temp = new Object();
temp["input"] = "acf[field_5701b4ecd11d1]";//end date
temp["message"] = "End date must be after start date.";
json.errors.push(temp);
}
}
// return
return json;
});
});
おかげで、! – Eckstein