javascript - How to determine if the date is full date and year only in JS -
i have regex code below validate valid date.
function isvaliddate(div) { var reg = /^((0?\d)|(1[012]))\/([012]?\d|30|31)\/\d{1,4}$/; var regs = /^\/\d{1,4}$/; var datefield = $(div).val(); if (datefield == "") { //alert('invalid inclusive dates.'); return false; } else { if (reg.test(datefield) == false && regs.test(datefield) == false) { // alert('invalid inclusive dates.'); return false; } else { return true; } } }
i testing code can see have 2 condition in if (reg.test(datefield) == false && regs.test(datefield) == false) {
. in line testing if date full date(1/1/2017) or (2017) only. if input 2 different dates return false because of condition. how achieve this?.
make day , month optional:
var reg = /^(?:(0?\d|1[012])\/(0?[1-9]|[12]\d|30|31)\/)?\d{1,4}$/; // ^^^ ^^
Comments
Post a Comment