html
1 2 3 | < input type = "text" name = "year" value = "" size = "4" placeholder = "2019" >年 < input type = "text" name = "month" value = "" size = "2" placeholder = "3" >月 |
jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | $( function () { // 今月 var date = new Date () ; var year = date.getFullYear() ; // 年 var month = date.getMonth() + 1 ; // 月 // 3ヶ月後 var date3Month = new Date () ; date3Month.setMonth(date3Month.getMonth() + 3); var add3Month = date3Month.getMonth() + 1 ; // 月 // 年(今年と来年以外の値が入ったら消す) $( "input[name='year']" ).blur( function (){ if (!$( this ).val().match(year) && !$( this ).val().match(year + 1)){ $( this ).val( "" ); } }); // 月(3ヶ月後以外の月が入れられたら消す) //var month = 7; //var add3Month = 10; //7 8 9 10 $( "input[name='month']" ).blur( function (){ if (month > add3Month) { //var month = 11; //var add3Month = 1; //11 12 1 の月の場合。(3ヶ月先が1月とか小さい値になる場合) if ( $( this ).val() < month && $( this ).val() > add3Month ){ $( this ).val( "" ); } } else { //var month = 7; //var add3Month = 10; //7 8 9 10 の月の場合。(3ヶ月先が今月より大きい値になる場合) if ( $( this ).val() < month || $( this ).val() > add3Month ){ $( this ).val( "" ); } } }); }); |