how to use monthNames in jqgrid when validating date?

后端 未结 2 549
醉话见心
醉话见心 2021-01-24 03:00

In my jqgrid when i am clicking on add new record i have date field prepopulated with current date. Format of the date is yyyy-MMM-d (e.g. 2010-Jan-23). Date is required field a

2条回答
  •  伪装坚强ぢ
    2021-01-24 03:43

    I was facing similar issue. I have resolved it using custom function for validation. I am suing date format as '23-Jan-05'. You may modify it to suit your required date format.

    function datecheck(value, colname) {
                var dateParts = value.split("-");
                var datePart = dateParts[0];
                var mounth = dateParts[1];
                var yearPart = dateParts[2];
    
                var mounths = $.jgrid.formatter.date.monthNames;
                var monthPart = -1;
                for (var i = 0; i < 12; i++) {
                    if (mounth === mounths[i]) {
                        monthPart = i + 1;
                        break;
                    }
                }
                var dateText = monthPart + '-' + datePart + '-' + yearPart;
                var date = Date.parse(dateText);
                if (isNaN(date)) 
                    return [false,"Invalid date. Format expected: dd-mmm-yy. (23-Jul-05)"];
                else 
                return [true,""];                  
    
            }
    

    JQGrid column details:

    colModel: [name: 'JoiningDate', align: "center", editable: true, 
               editrules: { custom: true, custom_func: datecheck },
               formatter: 'date', formatoptions: { srcformat: 'y-m-d', newformat: 'd-M-y' }, edittype: 'text', editable: true,
               editoptions: { dataInit: function (el) { setTimeout(function () { $(el).datepicker({ dateFormat: 'd-M-y' }); }, 200); } }
                },
    

    Hope this helps.

    Regards,

    Abhilash

提交回复
热议问题