babyPG babyPG - 3 months ago 20
jQuery Question

jquery check input date is valid

User input date in textbox (format: YYYYMMDD), I want to check the authenticity of the user input.

HTML

<input type="text" id="datepicker"/>


jQuery

$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "-200:+0"
});

$('#datepicker').blur(function () {
//how to code
});


function
onblur
, how to check value.
such as (datepicker range year 1816-2016)


1. 20170130 => invalid
2. 20132901 => invalid
3. 20130199 => invalid
4. 20160130 => valid
5. 20120305 => valid
6. 79745641 => invalid

Answer

You can try this in datepicker blur function

$("#datepicker").blur(function(){
        var date = $(this).datepicker('getDate')
        val = $(this).val();
       year =  date.getFullYear();
        val1 = Date.parse(val);
        if (year < 2016){
           alert("error")
        }
        else{
           console.log(val1);
        }
    });
});