Hello,
does anyone know how i can validate a date field using javascript. The field is a text field that accepts a date in the format yyyy-mm-dd.
i appreciate your help.
Thanks.
Hello,
does anyone know how i can validate a date field using javascript. The field is a text field that accepts a date in the format yyyy-mm-dd.
i appreciate your help.
Thanks.
A quick summary and a practical approach.
As asked, the goal is to validate a text field that holds dates in yyyy-mm-dd. 's format-only check is useful to ensure the shape of the string, but it won't catch impossible dates (for example February 30, month 00, or rolled-over days). 's calendar question and 's reply are also on point: a calendar widget prevents many bad inputs, but you still need deterministic validation and a server-side check.
Recommended client-side routine: confirm the string splits into three numeric parts, check numeric ranges (month 1–12), then verify the actual calendar date by constructing a Date from the components and comparing the fields back. That method automatically handles month lengths and leap years without brittle giant regexes.
Example validator:
function isValidISODate(s) {
if (typeof s !== 'string') return false;
const parts = s.split('-');
if (parts.length !== 3) return false;
const [y, m, d] = parts.map(Number);
if (!Number.isInteger(y) || !Number.isInteger(m) || !Number.isInteger(d)) return false;
if (m < 1 || m > 12) return false;
const dt = new Date(y, m - 1, d);
return dt.getFullYear() === y && dt.getMonth() === m - 1 && dt.getDate() === d;
} Practical notes: prefer native controls (<input type="date"> or datetime-local) when available for better UX (MDN input date, MDN datetime-local). Avoid relying on Date.parse/new Date(string) for raw ISO-like strings because parsing behavior can vary across engines — parse parts yourself for consistency (MDN Date.parse). Always repeat validation on the server side.
Jump to Post— anuradhu 0find here the regular expression...
if you are still not able to figure it out..let me know...../^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
find here the regular expression...
if you are still not able to figure it out..let me know.....
/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/
hello can any one tell me how to validate date and time. i am selecting date and time from calendar window .then how should i validate it .
If you are selecting it from a Calendar widget, won't it be automatically validated? Of course you can't pick out '123/123/123' from it, can you?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.