How to validate a date?

前端 未结 12 1795
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 04:13

I\'m trying to test to make sure a date is valid in the sense that if someone enters 2/30/2011 then it should be wrong.

How can I do this with any date?

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 04:38

    var isDate_ = function(input) {
            var status = false;
            if (!input || input.length <= 0) {
              status = false;
            } else {
              var result = new Date(input);
              if (result == 'Invalid Date') {
                status = false;
              } else {
                status = true;
              }
            }
            return status;
          }
    

    this function returns bool value of whether the input given is a valid date or not. ex:

    if(isDate_(var_date)) {
      // statements if the date is valid
    } else {
      // statements if not valid
    }
    

提交回复
热议问题