简体   繁体   中英

Flutter: Textformfield validator not working

I am using TextFormField in flutter. i want to validate the TextFormField that value should be less than 100 or it should be character 'a' alone

I tried

validator: (value) {
                  if (value.length == 0  ) {
                    return ('value is required!');
                  }
                  else if(value != "a"  || value != "A" || int.parse(value) < 0.0 || int.parse(value) > 100  )
                    {
                      return ('value should between 0 to 100 if absent put "A"!');
                    }
                },

But it not working.Anyone please help me. Thanks in advance

Regards, Sathish

Remember to return null when the validator should not display an error.

    if (value.isEmpty) return 'value is required';
    if (value.toLowerCase() == 'a') return null;

    var intValue = int.tryParse(value);
    if (intValue == null) { // not a number
      return 'value should be between 0-100 if absent put A';
    } else {
      return intValue > 100 ? 'value should be between 0-100' : null;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM