Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make age validation in flutter

My goal is to check users age by entered birthday and return error if user is not 18 years old or older. But i have no idea how to do that. Date format is "dd-MM-yyyy". Any ideas how to do that?

like image 936
Ivanius Avatar asked Apr 16 '20 12:04

Ivanius


2 Answers

Package

To easily parse date we need package intl:

https://pub.dev/packages/intl#-installing-tab-

So add this dependency to youd pubspec.yaml file (and get new dependencies)

Solution #1

You can just simple compare years:

bool isAdult(String birthDateString) {
  String datePattern = "dd-MM-yyyy";

  DateTime birthDate = DateFormat(datePattern).parse(birthDateString);
  DateTime today = DateTime.now();

  int yearDiff = today.year - birthDate.year;
  int monthDiff = today.month - birthDate.month;
  int dayDiff = today.day - birthDate.day;

  return yearDiff > 18 || yearDiff == 18 && monthDiff >= 0 && dayDiff >= 0;
}

But it's not always true, because to the end of current year you are "not adult".

Solution #2

So better solution is move birth day 18 ahead and compare with current date.

bool isAdult2(String birthDateString) {
  String datePattern = "dd-MM-yyyy";

  // Current time - at this moment
  DateTime today = DateTime.now();

  // Parsed date to check
  DateTime birthDate = DateFormat(datePattern).parse(birthDateString);

  // Date to check but moved 18 years ahead
  DateTime adultDate = DateTime(
    birthDate.year + 18,
    birthDate.month,
    birthDate.day,
  );

  return adultDate.isBefore(today);
}
like image 126
Boken Avatar answered Nov 15 '22 11:11

Boken


The best age validation I have ever come up with is based on Regex.
The below logic covers all the breakpoint related age.

// regex for validation of date format : dd.mm.yyyy, dd/mm/yyyy, dd-mm-yyyy
RegExp regExp = new RegExp(
    r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$",
    caseSensitive: true,
    multiLine: false,
  );

//method to calculate age on Today (in years)
  int ageCalculate(String input){
  if(regExp.hasMatch(input)){
  DateTime _dateTime = DateTime(
      int.parse(input.substring(6)),
      int.parse(input.substring(3, 5)),
      int.parse(input.substring(0, 2)),
    );
    return DateTime.fromMillisecondsSinceEpoch(
                DateTime.now().difference(_dateTime).inMilliseconds)
            .year -
        1970;
  } else{
    return -1;
  }
}

void main() {
// input values and validations examples
  var input = "29.02.2008";
  print("12.13.2029 : " + regExp.hasMatch("12.13.2029").toString());
  print("29.02.2028 : " + regExp.hasMatch("29.02.2028").toString());
  print("29.02.2029 : " + regExp.hasMatch("29.02.2029").toString());
  print("11/12-2019 : " + regExp.hasMatch("11/12-2019").toString());
  print("23/12/2029 : " + regExp.hasMatch("23/12/2029").toString());
  print("23/12/2029 : " + regExp.hasMatch(input).toString());
  print("sdssh : " + regExp.stringMatch("sdssh").toString());   
  print("age as per 29.02.2008 : " + ageCalculate(input).toString());
}

Output

 12.13.2029 : false
 29.02.2028 : true
 29.02.2029 : false
 11/12-2019 : false
 23/12/2029 : true
 23/12/2029 : true
 sdssh : null
 age as per 29.02.2008 : 12

I hope you will find this useful. :)

like image 29
Sanket Vekariya Avatar answered Nov 15 '22 10:11

Sanket Vekariya