Rutvik Gumasana
Rutvik Gumasana

Reputation: 1630

How to add range validation for year in flutter

I've tried to add year range validation it must be between 1850 to the current year. i've tried but it's not working.

Here is the code I've tried

  String validateestablishedyear(String value) {
    var date = new DateTime.now();
    int currentYear = date.year;
    int userinputValue = 0;
    print('currentYear = $currentYear');
    print('input value = $value');
    print('value = ${value is int}');
    print('value = ${value is String}');

    if (value is String && value.length == 0) {
      return "Established Year is Required";
    } else {
      userinputValue = int.parse(value);
    }

    if (userinputValue < 1850 && userinputValue >= currentYear) {
      return "Year must be between 1850 and $currentYear";
    }
    return null;
  }

Upvotes: 1

Views: 504

Answers (1)

Jay Mungara
Jay Mungara

Reputation: 7148

Just change your code as below,

if (userinputValue < 1850 || userinputValue >= currentYear) {
  return "Year must be between 1850 and $currentYear";
}

You're logic is wrong. you should have use || instead of &&

Upvotes: 2

Related Questions