jQuery - datepicker.parseDate on 'mm/y' causing Invalid Date

99封情书 提交于 2019-12-11 03:19:32

问题


I have a validate method which seems pretty simple because I use something very similar when using the format mm/dd/yy, but when I use mm/y...I keep getting an invalid date.

Here is my validation:

function validateDate(dateField) {
    try{
        $.datepicker.parseDate('mm/y', dateField, null);
    }
    catch(error){
        alert(error);
    }
}

If I pass in a date like 05/11...this logic complains that the date is invalid. If I change the format to mm/dd/yy and enter 05/11/2011...then it says it is valid.

Am I missing something when trying to validate a mm/y pattern?


回答1:


Unfortunately, you need to supply at least a month and day for the datepicker to parse your date properly. If you leave the year out, it defaults to the current year, so parsing "06/15" as "dd/mm" will give you 2011-06-15. But if either month or day are omitted, they default to -1 and will produce an invalid date.

EDIT:

If you are just trying to confirm that the user entered a valid month and year, do this:

$.datepicker.parseDate('dd/mm/y', "01/" + dateField, null);


来源:https://stackoverflow.com/questions/5956766/jquery-datepicker-parsedate-on-mm-y-causing-invalid-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!