Javascript Date Validation ( DD/MM/YYYY) & Age Checking

前端 未结 16 1527
遇见更好的自我
遇见更好的自我 2020-12-01 11:09

I\'ve started to work on Javascript recently. What I am testing is checking the DoB in valid format. Next step will be checking the age.

What my HTML code includes

相关标签:
16条回答
  • 2020-12-01 11:47

    When we put only pattern it's not simple to check every possible date combination. Users can enter valid numbers like 99/99/9999 but it's not a valid date. Even If we limit days and months to a more restrictive value (30/31 days and 0-12 months) we still may get a case where we have leap year, febraury etc. and we cannot properly validate them using regex. So the better approach is to use a date object itself.

    let InputDate = "99/99/9999"
    let pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
    let editDate = InputDate.replace("\/","-")
    
    let dateValidation = function validation(){
      
    if(pattern.test(InputDate) && new Date(editDate) == 'Invalid Date'){
      return false;
    }else{
      return true;
    }
    
    }
    
    console.log(dateValidation()) //Return false

    0 讨论(0)
  • 2020-12-01 11:50

    To get the values use pattern.exec() instead of pattern.test() (the .test() returns a boolean value).

    0 讨论(0)
  • 2020-12-01 11:52

    I use the code I found @.w3resources

    The code takes care of

    • month being less than 12,

    • days being less than 32

    • even works with leap years. While Using in my project for leap year I modify the code like

      if ((lyear==false) && (dd>=29))
      {
      alert('Invalid date format!');
      return false;
      }

      if ((lyear==false) && (dd>=29))
      {
      alert('not a Leap year February cannot have more than 28days');
      return false;
      }

    Rather than throwing the generic "Invalid date format" error which does not make much sense to the user. I modify the rest of the code to provide valid error message like month cannot be more than 12, days cannot be more than 31 etc.,

    The problem with using Regular expression is it is difficult to identify exactly what went wrong. It either gives a True or a false-Without any reason why it failed. We have to write multiple regular expressions to sort this problem.

    0 讨论(0)
  • 2020-12-01 11:58

    I suggest using moment.js which provides an easy to use method for doing this.

    interactive demo

    function validate(date){
        var eighteenYearsAgo = moment().subtract(18, "years");
        var birthday = moment(date);
    
        if (!birthday.isValid()) {
            return "invalid date";    
        }
        else if (eighteenYearsAgo.isAfter(birthday)) {
            return "okay, you're good";    
        }
        else {
            return "sorry, no";    
        }
    }
    

    To include moment in your page, you can use CDNJS:

    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
    
    0 讨论(0)
  • 2020-12-01 11:58

    If you're using moment then that's the single line code:

    moment(date).format("DD/MM/YYYY").isValid()

    0 讨论(0)
  • 2020-12-01 12:01
             if(!/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{2}$/.test($(this).val())){
                         alert('Date format incorrect (DD/MM/YY)');
                         $(this).datepicker('setDate', "");
                         return false;
                    }
    

    This code will validate date format DD/MM/YY

    0 讨论(0)
提交回复
热议问题