Monday, April 15, 2024

Javascript From and To datepicker validation

  var now = new Date();

    var month = (now.getMonth() + 1);

    var day = now.getDate();

    if (month < 10)

        month = "0" + month;

    if (day < 10)

        day = "0" + day;

    var dateFrom = '01' + '-' + month + '-' + (now.getFullYear());

    var dateTo = day + '-' + month + '-' + now.getFullYear();

    $('#dateFrom').val(dateFrom);

    $('#dateTo').val(dateTo);

    $('[id*=dateFrom]').datepicker({

        changeMonth: true,

        changeYear: true,

        format: "dd-mm-yyyy",

        language: "tr",

        todayHighlight: true,

        autoclose: true,

        isRTL: true,

        endDate: dateTo

    })

    .on('change', function (date) {

        if ($("#dateFrom").val() == '') {

            $("#dateFrom").datepicker("setDate",('01' + '-' + month + '-' + (now.getFullYear())));

        }

        var arr1 = $('#dateFrom').val().split('-');

        var arr2 = $('#dateTo').val().split('-');

        if ((new Date(arr1[2], arr1[1] - 1, arr1[0])) > (new Date(arr2[2], arr2[1] - 1, arr2[0]))) {

            //$("#dateTo").val($("#dateFrom").val());

            $("#dateTo").datepicker("setDate", $('#dateFrom').val());

            alert("From date must be less than To date!!!");

        }

        else {

            $('#dateTo').datepicker('destroy');

            $('#dateTo').datepicker({

                changeMonth: true,

                changeYear: true,

                format: "dd-mm-yyyy",

                language: "tr",

                todayHighlight: true,

                autoclose: true,

                isRTL: true,

                startDate: $('#dateFrom').val(),

                endDate: new Date()


            });

        }

    });


    $('[id*=dateTo]').datepicker({

        changeMonth: true,

        changeYear: true,

        format: "dd-mm-yyyy",

        language: "tr",

        todayHighlight: true,

        autoclose: true,

        isRTL: true,

        endDate: new Date()

    })

    .on('change', function (date) {

    if ($("#dateTo").val() == '') {

        $("#dateTo").val(day + '-' + month + '-' + (now.getFullYear()));

    }


    var arr1 = $('#dateFrom').val().split('-');

    var arr2 = $('#dateTo').val().split('-');

    if ((new Date(arr1[2], arr1[1] - 1, arr1[0])) > (new Date(arr2[2], arr2[1] - 1, arr2[0]))) {

        //$("#dateFrom").val($("#dateTo").val());

        $("#dateFrom").datepicker("setDate", $('#dateTo').val());

        alert("To date must be greater than From date!!!");

    }

    //    $('#dateFrom').datepicker('destroy');

    //    $('#dateFrom').datepicker({

    //        changeMonth: true,

    //        changeYear: true,

    //        format: "dd-mm-yyyy",

    //        language: "tr",

    //        todayHighlight: true,

    //        autoclose: true,

    //        isRTL: true,

    //        endDate: $('#dateTo').val()

    //    });

    });


or Correct way


var now = new Date();

    var month = (now.getMonth() + 1);

    var day = now.getDate();

    if (month < 10)

        month = "0" + month;

    if (day < 10)

        day = "0" + day;

    var dateFrom = '01' + '-' + month + '-' + (now.getFullYear());

    var dateTo = day + '-' + month + '-' + now.getFullYear();

    $('#dateFrom').val(dateFrom);

    $('#dateTo').val(dateTo);

    $('[id*=dateFrom]').datepicker({

        changeMonth: true,

        changeYear: true,

        format: "dd-mm-yyyy",

        language: "tr",

        todayHighlight: true,

        autoclose: true,

        isRTL: true,

        endDate: dateTo

    });


    $('[id*=dateTo]').datepicker({

        changeMonth: true,

        changeYear: true,

        format: "dd-mm-yyyy",

        language: "tr",

        todayHighlight: true,

        autoclose: true,

        isRTL: true,

        endDate: new Date()

    });


    UserVsRoleddl();

    GetLoggedInUserDetails();

$('[id$=hfUserVsRoleValue]').val($("#ddlUserVsRole option:selected").val());

    $('[id$= hfUserVsRoleText ]').val($("#ddlUserVsRole option:selected").text());


    //$('#PRFIOption').attr('hidden', 'hidden');

    //Initialization ends


    $("#dateFrom").change(function () {

        if ($("[id$=dateFrom]").val() == '') {

            $("#dateFrom").datepicker("setDate", '01' + '-' + month + '-' + (now.getFullYear()));

        }

        var arr1 = $('#dateFrom').val().split('-');

        var arr2 = $('#dateTo').val().split('-');

        if ((new Date(arr1[2], arr1[1] - 1, arr1[0])) > (new Date(arr2[2], arr2[1] - 1, arr2[0]))) {

            $("#dateFrom").datepicker("setDate", $('#dateTo').val());

            alert("From date must be less than To date...");

        }

        //GetDashBoardSummary();

        //CourtApplicationCard();

        //--Hari---

        //debugger;

        //var ActiveTab = $('#myTab').find('a[aria-selected=true]').attr('id');


        //if (ActiveTab == 'nav-DisposalTrial-tab') {

            //DisposalTrialGraph();

        //}

        //if (ActiveTab == 'nav-UnderTrial-tab') {


            //PendingTrialGraph();

        //}

    });

    $("#dateTo").change(function () {

        var now = new Date();

        var month = (now.getMonth() + 1);

        var day = now.getDate();

        if (month < 10)

            month = "0" + month;

        if (day < 10)

            day = "0" + day;

        if ($("[id$=dateTo]").val() == '') {

            $("#dateTo").datepicker("setDate", day + '-' + month + '-' + now.getFullYear());

        }

        var arr1 = $('#dateFrom').val().split('-');

        var arr2 = $('#dateTo').val().split('-');

        if ((new Date(arr1[2], arr1[1] - 1, arr1[0])) > (new Date(arr2[2], arr2[1] - 1, arr2[0]))) {

            $("#dateTo").datepicker("setDate", $('#dateFrom').val());

            alert("To date must be greater than From date...");

        }

        //GetDashBoardSummary();

        //CourtApplicationCard();

        //--Hari---

        //var ActiveTab = $('#myTab').find('a[aria-selected=true]').attr('id');


        //if (ActiveTab == 'nav-DisposalTrial-tab') {

            //DisposalTrialGraph();

        //}

        //if (ActiveTab == 'nav-UnderTrial-tab') {


            //PendingTrialGraph();

        //}


    });

No comments:

Post a Comment