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

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?

Related

Regex for time input [duplicate]

This question already has answers here:
How can I make part of regex optional?
(2 answers)
Closed 2 years ago.
I can have the following kinds of value I receive from the input:
1w 1d 1h 1m
or
1н 1д 1ч 1м
Where the week (w) and day (d) options are optional and the rest is obligatory. I came up with the following regex:
/^(([0-9][w,н])\s([0-9][d,д]))\s?([0-24][h,ч])\s([0-60][m,м])$/
But it's not working.
I think I messed up the optional part and I'm not quite sure about the capturing groups as well, so any help here would be greatly appreciated.
This works: (?:\d[wн]\s)?(?:\d[dд]\s)?(:?\d{1,2}[hч])\s(:?\d{1,2}[mм])
See: https://regex101.com/r/X4ObPD/2
Try:
/^(?:\d+[wн]\s+\d+[dд]\s+)?\d+[чh]\s+\d+[мm]$/
You are using the regex class operator [..] wrong. I see your intention to set the allowed range, but this is not how it works. You can only set the allowed character set here. So [0-9] simply means, all characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are allowed. How many of them is specified afterwards, e.g. [0-9]+ means one or more. In your case I'd say "one or two" is what you want for minutes and hours like [0-9]{1,2}.
The following regex works as expected:
^(([0-9]+)[wн]\s([0-9]+)[dд]\s)?(([0-9]{1,2})[hч]\s)(([0-9]{1,2})[mм])$
Please note that you have to do the value validation (so hours and minutes are in the right range) afterwards programmatically.

How to validate date in Javascript using Regex? [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 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">

Why is my regular expression for a valid date not working? [duplicate]

This question already has answers here:
Match exact string
(3 answers)
What is the MM/DD/YYYY regular expression and how do I use it in php?
(9 answers)
Closed 4 years ago.
I am trying to learn how to use regular expressions. Currently, I am creating my own regular expression using JavaScript to test for a date in the format of MM-DD-YYYY.
Here is my code:
// regex for testing valid date
var regex = new RegExp("[0-9]{2}\-[0-9]{2}\-[0-9]{4}");
regex.test("113-12-1995");
Unfortunately, this is outputing to true and I cannot figure out why. I am under the impression that {2} means it must be two digits and no more or less. It seems like it is behaving as if I had put a {2,} which would correlate to at least two digits, but that isn't what I want.
Additionally, how would I test to see if the value of the first two digits are greater than 12?

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).

Date bookmarklet needs leading zero on single digit months and days

I have a bookmarklet I use to check daily log files. However the bookmarklet I use only delivers the month and day in single digits, however the log files use double digits.
For example my bookmarklet delivers:
http://url/log/2009-5-4_localcontrol-story.log,
while the log file actually lives at:
http://url/log/2009-05-04_localcontrol-story.log
Below is my current code:
javascript:d=new%20Date();window.open("http://url/log/"+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+(d.getDate())+"_localcontrol-story.log",%20"_self");
Can you tell me an adaptation to this so I get my month and date in 2 digit format with the leading zero if necessary?
it's kind of a pain, but what I've done is to do stuff like this:
("0"+d.getDate()).slice(-2)
(add a leading zero, and slice(-2) takes the last 2 characters)

Categories