Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker: validate date

i have set a jquery class:

$(function() {
            $( ".datepickerIT" ).datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showAnim: "clip",
                dateFormat: "dd/mm/yy",
                minDate: "01/01/1925",
                maxDate: "31/12/2050",
                changeMonth: true,
                changeYear: true,
                yearRange: "1925:2050",
                regional: "it"                      
            });
        });

I want to add a date check control that alert if user input not is a valid date

How can add to the class ".datepickerIT" a check like this?

onClose: function(dateText, inst) {
        try {
            $.datepicker.parseDate('dd/mm/yy', dateText);
        } catch (e) {
            alert(e);
        };

And what plugin i must include into head section?

like image 706
Manlio Avatar asked Dec 17 '14 14:12

Manlio


1 Answers

You can validate the date in following ways (you don't need a plugin for it):-

$(document).ready(function(){
    $("#datepicker").datepicker();
    $("#datepicker").blur(function(){
        val = $(this).val();
        val1 = Date.parse(val);
        if (isNaN(val1)==true && val!==''){
           alert("error")
        }
        else{
           console.log(val1);
        }
    });
});

Working fiddle

UPDATE: Correct approach is mentioned by @Razan Paul

like image 179
Gaurav Kalyan Avatar answered Sep 27 '22 02:09

Gaurav Kalyan