Naveen Kumar
Naveen Kumar

Reputation: 1626

date format check with regex Jquery

I have a form when submitted checks for null value and incorrect format of data. The issue here is even when the format is correct it throws the error. The date format i want is dd/mm/yyyy.

$("#sche_inter_form").submit(function(e){
if( ($("#inter_date").val()==="") || checkdateFormat()) {
     $("#inter_date").css({"border-bottom":" 1px solid #dd4b39"});
     e.preventDefault(e);
     }
function checkdateFormat(){
   var date = $("#inter_date").val();
   console.log(date);
   var re= new RegExp("/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/");
     var result = re.test(date);
     if (result) {
        return false;
            }
     else{
        return true;
         }
        }

Upvotes: 0

Views: 1038

Answers (1)

Rahul Desai
Rahul Desai

Reputation: 15501

Your regex is not matching a sample date like 11/11/2000.

Use this regex:

new RegExp("^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2,}$")

Upvotes: 1

Related Questions