0

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;
         }
        }
Naveen Kumar
  • 1,476
  • 5
  • 28
  • 52
  • 4
    Possible duplicate of [date format validation with jquery](http://stackoverflow.com/questions/25075418/date-format-validation-with-jquery) – toesslab Jan 18 '17 at 06:42
  • 1
    Possible duplicate of [How do I validate a date in this format (yyyy-mm-dd) using jquery?](http://stackoverflow.com/questions/18758772/how-do-i-validate-a-date-in-this-format-yyyy-mm-dd-using-jquery) – Sorangwala Abbasali Jan 18 '17 at 07:11

1 Answers1

1

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,}$")
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
  • @NaveenKumar The `/`'s surrounding the regex are not required in `RegExp()`. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – Rahul Desai Jan 18 '17 at 07:02
  • But without that it throws error unexpected token *caret symbol* – Naveen Kumar Jan 18 '17 at 07:05