How to check if a string is a legal “dd/mm/yyyy” date?

前端 未结 7 1948
遥遥无期
遥遥无期 2020-12-01 15:05

Given a string str, how could I check if it is in the dd/mm/yyyy format and contains a legal date ?

Some examples:

bla bla          


        
7条回答
  •  北海茫月
    2020-12-01 15:54

    you can use regular exp to validate date . try like this :

      re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
    if(form.mydate.value != '' && !form.mydate.value.match(re))
      //do something here
    

    note: this will only work for dd/mm/yyyy

    for exact match of your requirement use

     re = /^\d{2}\/\d{2}\/\d{4}$/; 
    

提交回复
热议问题