How to validate date in Javascript using Regex? [duplicate] - javascript

This question already has answers here:
Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
(27 answers)
Closed 3 years ago.
I need to make JavaScript function to validate date that users put inside input field. The valid date format needs to be yyyyMMdd. How to write regex code to validate this input date format? This date formats are valid: 2019/12/01 and 2019-12-01. All other formats are not allowed.

This is interesting topic... it starting with simple solution:
\d{4}(-|\/)\d{2}\1\d{2}
In this solution we are having 4 numbers than - or /, than 2 numbers, than delimiter used before (- or /) and than 2 more numbers... which is much better solution than
\d{4}[-\/]\d{2}[-\/]\d{2}
because in second one 2019-12/01 will pass validation, in first one will not pass...
but when you brainstorm more on that topic... you probably want validation to be even smarter than that so next step will be
[12]\d{3}(-|\/)\d{2}\1\d{2}
on this way year must start with 1 or 2... than sky is the limit what else you will include...
Sorry for too much writing but validation is always interesting topic

I need to make JavaScript function to validate date that users put
inside input field.
If you can, you might be better off using <input type="date"> instead.
Working Example:
<input type="date">

Related

Validate dateFormat in dd/mm/yyyy using regex [duplicate]

This question already has answers here:
Javascript date regex DD/MM/YYYY
(13 answers)
Closed 7 years ago.
I want to validate the format of the date value entered by a user using regex with javascript.
My regex doesn't allow the '/' character , /[^0-9\.]/g,''
But I want to let '/' pass the regex test too. What modification do I need to make here?
Modified from this answer you can be pretty exact with this. This works for the years 1000-9999, is Proleptic Gregorian and assumes that we won't change how leap-years work until the year 9999 ;)
^(?:(?:(?:0[1-9]|1\d|2[0-8])/(?:0[1-9]|1[0-2])|(?:29|30)/(?:0[13-9]|1[0-2])|31/(?:0[13578]|1[02]))/[1-9]\d{3}|29/02/(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$
Debuggex Demo
"20/11/1992".match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)
The above snippet should do, but there are too many validations to be performed on dates, so I wouldn't recommend regex.
Instead, I'd say do it like most websites do and place 3 combo boxes (dd/mm/yyyy), and allow the user to select a date, then you validate that date using the Date() constructor (if the values haven't changed, the date is correct).
note: the answer is based upon the assumption that you don't want to use any of the existing libraries (or the native validation provided by browser when using input[type="date"])
You can use this regex:
/(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/
This validates the date with format dd/mm/yyyy and also checks for leap years.
It depends on how strict you need to be? I thing that simple:
/[0-3]\d\W[01]\d\W(?>19|20)\d{2}/g
should be sufficient.
day: [0-3]\d 2 digits, first 0-3, second any number \d
month: [01]\d 2 digits, first 0 or 1, second any number
year: (?>19|20)\d{2} 4 digits, starts with 19 or 20 (for 19th and 20th century) and next any digit two times {2}
Also note, I used \W to match single non-word character as workaround to match /. Are you sure that you cannot use escaped slash \/ instead?

Jquery validation method for date in dd/mm/yyyy format [duplicate]

This question already has answers here:
Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
(27 answers)
Closed 8 years ago.
I want to add new validation method to jquery validation that validates date in dd/mm/yyyy format ex(28/01/2015)
I tried :
$.validator.addMethod("dateFormat",function(value, element) {
return value.match(/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012]) [\/\-]\d{4}$/);
});
which is not working as expected.. any better way of doing it?
I'm sorry I can't provide a more personal answer, but there's already a lot on this. Does this link help?
Javascript - Regex to validate date format
Example from linked answer:
var dateReg = /^\d{2}([./-])\d{2}\1\d{4}$/
"22-03-1981".match(dateReg) // matches
"22.03-1981".match(dateReg) // does not match
"22.03.1981".match(dateReg) // matches
Another link that might help:
Javascript date regex DD/MM/YYYY

Javascript: is there a shorter way to validate a date with regex? I want to allow m/d/yyyy as well as mm/dd/yyyy [duplicate]

This question already has answers here:
How to validate a date?
(11 answers)
Closed 8 years ago.
My goal is to validate a user-entered date in javascript.
I am basing my code on https://stackoverflow.com/a/6178341/3255963, but that code was designed to only validate mm/dd/yyyy.
if(!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString))
return false;
I want to also allow users to enter m/d/yyyy (in other words no leading zeros).
It appears the following works but I was wondering if there is a way to do this with regex in one line.
if(
!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString) &&
!/^\d{1}\/\d{1}\/\d{4}$/.test(dateString) &&
!/^\d{1}\/\d{2}\/\d{4}$/.test(dateString) &&
!/^\d{2}\/\d{1}\/\d{4}$/.test(dateString)
)
return false;
PS Another portion of the linked script verifies other aspects of the input, but I'm not modifying that part.
You can specify minimum and the maximum number of characters to be matched, like this
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
Note: As #Matt suggested in the comments, using RegEx to validate date strings, is not a foolproof way.

javascript - regular expression to validate date in mm/dd/yyyy format [duplicate]

This question already has answers here:
Regular Expression to match valid dates
(16 answers)
date formatting with/without Moment.js
(4 answers)
Closed 9 years ago.
I'm not good at regular expression. I got code to validate dd/mm/yyyy format which also validates leap year, I tried to modify to get it work for mm/dd/yyyy, but they all failed.
Can some one change it to validate mm/dd/yyyy format?
Regular Expression:
^(((0[1-9]|[12]\d|3[01])/(0[13578]|1[02])/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)/(0[13456789]|1[012])/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])/02/((19|[2-9]\d)\d{2}))|(29/02/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$
Answer
Hi All, thanks for the help from all of you, finally parsing again the regular expression, i got my answer to validate mm/dd/yyyy format
Regular Expression:
/^(((0[13578]|1[02])/(0[1-9]|[12]\d|3[01])/((19|[2-9]\d)\d{2}))|((0[13456789]|1[012])/(0[1-9]|[12]\d|30)/((19|[2-9]\d)\d{2}))|(02/(0[1-9]|1\d|2[0-8])/((19|[2-9]\d)\d{2}))|(02/29/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g
Try with
function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}
But better to use regular expression use Date object from your date string and then validate it.

What regular expression should I use to match this date pattern: DAY.month.YEAR?

I have this date format:
DAY.month.YEAR (today: 28.06.2011)
I will need a Regular Expression (RegEx) pattern for matching this date format.
Can anyone post a solution for this problem?
Derived from http://www.regular-expressions.info/dates.html:
(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d
This matches a date in dd.mm.yyyy format from between 01.01.1900 and 31.12.2099. It will, however, still match invalid dates, because validating leap years, for example, can not be done with regex (at least not very easily).
However, a regex is probably unnecessary. Javascript example:
var date = "28.06.2011".split("."); // split up the date by the dots
// parse the components into integers
var day = parseInt(date[0]);
var month = parseInt(date[1]);
var year = parseInt(date[2]);
// if you want the date in a date object, which will fix leap years (e.g. 31.02 becomes 03.03
var date = new Date(year, month - 1, day);
Note that when creating a date object, month starts at zero.
Which method you use depends on what you need this for. If you want to find dates in a text, use the regex. If you simply want to parse the date into a date object, use the second method. Some extra validation is possibly necessary to make sure the date is valid, as the javascript Date object does not care about February having 31 days, it simply wraps over to 3. of March.
If you wish to match the format of the date, than it will be enough to use:
\d\d\.\d\d\.\d\d\d\d
However, as David Hall pointed out in his comment to your question, you will still need to validate the date in your code. Doing this in a regex isn't easy, as you can see from Harpyon answer which - still making a "preliminary check that filter out many wrong possiblities" - also accepts 31.02.2011 as a valid date and misses out on the French revolution (14 July 1789).

Categories