How can I validate that someone is over 18 from their date of birth?

后端 未结 6 1211
一向
一向 2020-12-31 16:47

I am doing validation for Driver\'s Date of birth, it should be minimum of 18 from the current date.

var Dates = $get(\'<%=ui_txtDOB.ClientID %>\');            


        
6条回答
  •  余生分开走
    2020-12-31 17:41

    After looking at various methods of doing this, I decided the simplest way was to encode the dates as 8-digit integers. You can then subtract today's code from the DOB code and check if it's greater than or equal to 180000.

    function isOverEighteen(year, month, day) {
      var now = parseInt(new Date().toISOString().slice(0, 10).replace(/-/g, ''));
      var dob = year * 10000 + month * 100 + day * 1; // Coerces strings to integers
    
      return now - dob > 180000;
    }
    

提交回复
热议问题