1/** 2 * Restrict Date of birth to today or earlier on My Account edit form. 3 */ 4(function ($) { 5 'use strict'; 6 7 function todayIso() { 8 var now = new Date(); 9 var month = String(now.getMonth() + 1).padStart(2, '0'); 10 var day = String(now.getDate()).padStart(2, '0'); 11 return now.getFullYear() + '-' + month + '-' + day; 12 } 13 14 function isDobField($input) { 15 if (!$input || !$input.length) { 16 return false; 17 } 18 19 var type = ($input.attr('type') || '').toLowerCase(); 20 var name = ($input.attr('name') || '').toLowerCase(); 21 var id = ($input.attr('id') || '').toLowerCase(); 22 var haystack = name + ' ' + id; 23 24 if (type === 'date') { 25 return true; 26 } 27 28 if (/birth|dob|date_of_birth|date-of-birth/.test(haystack)) { 29 return true; 30 } 31 32 var $label = $input.closest('.form-row').find('label').first(); 33 if ($label.length && /date of birth/i.test($label.text())) { 34 return true; 35 } 36 37 return false; 38 } 39 40 function applyMaxDate($input) { 41 var maxDate = todayIso(); 42 43 if ($input.attr('type') === 'date') { 44 $input.attr('max', maxDate); 45 } 46 47 if ($input[0]._flatpickr) { 48 $input[0]._flatpickr.set('maxDate', maxDate); 49 } 50 51 if ($input.hasClass('hasDatepicker') && $.datepicker) { 52 $input.datepicker('option', 'maxDate', 0); 53 } 54 } 55 56 function clampFutureValue($input) { 57 var value = ($input.val() || '').trim(); 58 if (!value) { 59 return; 60 } 61 62 var selected = new Date(value); 63 if (isNaN(selected.getTime())) { 64 return; 65 } 66 67 var today = new Date(); 68 today.setHours(0, 0, 0, 0); 69 selected.setHours(0, 0, 0, 0); 70 71 if (selected > today) { 72 $input.val(todayIso()); 73 $input.trigger('change'); 74 } 75 } 76 77 function initDobFields($scope) { 78 $scope.find('input').each(function () { 79 var $input = $(this); 80 if (!isDobField($input)) { 81 return; 82 } 83 84 applyMaxDate($input); 85 clampFutureValue($input); 86 87 $input.off('change.ksDob blur.ksDob').on('change.ksDob blur.ksDob', function () { 88 clampFutureValue($input); 89 }); 90 }); 91 } 92 93 $(function () { 94 var $form = $('form.woocommerce-EditAccountForm, form.edit-account'); 95 if (!$form.length) { 96 return; 97 } 98 99 initDobFields($form); 100 101 // Date pickers (Flatpickr / jQuery UI) may mount after DOM ready. 102 window.setTimeout(function () { 103 initDobFields($form); 104 }, 500); 105 }); 106})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.