0

is there a way to validate if a users input is a valid float? I need to verify that the user is inputting valid project estimates $$

only costs up to the hundreds is allowed, example: 1.99 .99

no commas are allowed no negative numbers only one period is allowed

I am using a TextFormField, and I am using the following keyboard: keyboardType: TextInputType.number,

I know I have to do some type of validation using the validator but I am stuck

Juan Casas
  • 268
  • 2
  • 13

1 Answers1

0
    validator: (str) {
                  if (str == '' || str == '.') {
                    return "Job estimate can't be empty";
                  }
                  if (str!.contains(',')) {
                    return 'No commas are allowed';
                  }
                  var strPlit = str.split('.');

                  print('strPlit: ${strPlit.length}');
                  if (strPlit.length != 1) {
                    if (strPlit.length > 2) {
                      return 'Only 1 period allowed';
                    }
                    if (strPlit[1].length > 2) {
                      return 'Estimate is only accurate to the hundreds';
                    }
                  }

                  return null;
                },
Juan Casas
  • 268
  • 2
  • 13