2

I am trying to validate date in js ("yyyy/mm/dd") format. After googling I found other date format checked but I can't get in this format.

Any one plz can help me out.

Here is my code.

function dateChecker()
{
    var date1, string, re;
    re = new RegExp("\d{4}/\d{1,2}/\{1,2}");

    date1 = document.getElementById("visitDate").value; 
    if(date1.length == 0)
    {
        document.getElementById("showError").innerHTML = "Plz Insert Date";
        document.getElementById("showError").style.color = "red";
    }
    else if(date1.match(re))
    {
        document.getElementById("showError").innerHTML = "Ok";
        document.getElementById("showError").style.color = "red";
    }
    else
    {
        document.getElementById("showError").innerHTML = "It is not a date";
        document.getElementById("showError").style.color = "red";
    }

}   
Krity Shrestha
  • 333
  • 1
  • 4
  • 10
  • Use `re=/\d{4}\/\d{1,2}\/\d{1,2}/` or `re=/^\d{4}\/\d{1,2}\/\d{1,2}$/` if the whole string should match. The `\{1,2}` is a typo, and if you need to use a constructor notation, then double the backslashes. Still, you do not validate a date this way, just check if the string looks like a date. – Wiktor Stribiżew Jan 13 '17 at 07:15
  • Use moment.js, regex is not best suited for date-validation. In moment.js, you can do something like, isValid function. – Mukul Jain Jan 13 '17 at 07:20
  • sorry! non of them works for me. – Krity Shrestha Jan 13 '17 at 07:32
  • Also see [*How to validate a date?*](http://stackoverflow.com/questions/5812220/how-to-validate-a-date/5812341#5812341). – RobG Jan 13 '17 at 08:46
  • If you use momentjs you can just do const date = moment('01-01-2021', 'DD-MM-YYYY', true) date.isValid() // true – Mak Suriya Jacobsen Jan 06 '21 at 08:49

2 Answers2

1

Try this:

var date = "2017/01/13";
var regex = /^[0-9]{4}[\/][0-9]{2}[\/][0-9]{2}$/g;
console.log(regex.test(date));    // true
console.log(regex.test("13/01/2017")); //false
console.log(regex.test("2017-01-13")); // false
Anurag Sinha
  • 1,014
  • 10
  • 17
0

If you use new RegExp then you must call compile on the resulting regular expression object.

re = new RegExp("\d{4}/\d{1,2}/\d{1,2}");
re.compile();

Alternatively you could write the regex this way which does not require compile to be called.

re = /\d{4}\/\d{1,2}\/\d{1,2}/;

EDIT

Note that the above regex is not correct (ie it can approve invalid dates). I guess the brief answer is, don't use regex to validate date times. Use some datetime library like momentjs or datejs. There is too much logic. For instance, how do you handle leap years, different months having different number of possible days, etc. Its just a pain. Use a library that can parse it, if it cant be parsed, its not a date time. Trust the library.

However you could get closer with something like this

re = /^\d{4}\/(10|11|12|\d)\/((1|2)?\d|30|31)$/;

Also if you want to get comfortable with regex, download Expresso

GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19