Subversion Repositories SmartDukaan

Rev

Rev 36845 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36845 Rev 36851
Line 24... Line 24...
24
 
24
 
25
// Restrict the Acquired Date picker to the last 7 days (today and the 7 days before it).
25
// Restrict the Acquired Date picker to the last 7 days (today and the 7 days before it).
26
// The form HTML is injected via AJAX, so set min/max lazily when the field is focused.
26
// The form HTML is injected via AJAX, so set min/max lazily when the field is focused.
27
// NOTE: native min/max only reliably greys out dates in Chrome's calendar; Firefox/Safari
27
// NOTE: native min/max only reliably greys out dates in Chrome's calendar; Firefox/Safari
28
// still let the user pick an out-of-range date, so we also enforce the range on 'change'.
28
// still let the user pick an out-of-range date, so we also enforce the range on 'change'.
29
$(document).on('focus', 'input[name="acquiredDate"]', function () {
29
$(document).on('blur', 'input[name="acquiredDate"]', function () {
30
    const today = new Date();
30
    const today = new Date();
31
    const minDate = new Date();
31
    const minDate = new Date();
32
    minDate.setDate(today.getDate() - 7);
32
    minDate.setDate(today.getDate() - 7);
33
    $(this).attr('max', toLocalYMD(today));
33
    $(this).attr('max', toLocalYMD(today));
34
    $(this).attr('min', toLocalYMD(minDate));
34
    $(this).attr('min', toLocalYMD(minDate));
35
});
35
});
36
 
36
 
37
// Browser-independent guard: if the selected Acquired Date is outside the last 7 days,
37
// Browser-independent guard: if the entered Acquired Date is outside the last 7 days,
38
// reject it immediately (clear the field) instead of waiting until form submit.
38
// reject it (clear the field) instead of waiting until form submit.
-
 
39
// IMPORTANT: validate on 'blur', NOT 'change'. When a user TYPES into a type="date"
-
 
40
// field, the year is entered one digit at a time, so the field momentarily holds
-
 
41
// complete-but-wrong dates (e.g. 0002-06-16 → 0020-... → 0202-... before 2026-...),
-
 
42
// each of which fires 'change'. Validating on 'change' would reject and clear those
-
 
43
// intermediate years, making manual entry impossible. 'blur' fires only once the
-
 
44
// user has finished editing the field.
39
$(document).on('change', 'input[name="acquiredDate"]', function () {
45
$(document).on('blur', 'input[name="acquiredDate"]', function () {
40
    const picked = $(this).val();
46
    const picked = $(this).val();
41
    if (!picked) return;
47
    if (!picked) return;
42
    const acq = parseLocalYMD(picked);
48
    const acq = parseLocalYMD(picked);
43
    const today = new Date();
49
    const today = new Date();
44
    today.setHours(0, 0, 0, 0);
50
    today.setHours(0, 0, 0, 0);