jQuery - datepicker.parseDate on 'mm/y' ca

2019-07-22 04:31发布

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条回答
欢心
2楼-- · 2019-07-22 05:10

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);
查看更多
登录 后发表回答