【问题标题】:How to check entered all values are white spaces or not?如何检查输入的所有值是否为空格?
【发布时间】:2020-09-16 02:12:26
【问题描述】:

我在 textField 中的验证有问题。 如果我在 textField 中输入的所有值都是空格,那么当我按下提交按钮时它应该会引发验证错误。 我试图用 isEmpty 解决但不工作。

TextFormField textField(BuildContext context) {
    return TextFormField(
        maxLines: 10,
        minLines: 6,
        controller: _noteTextController,
        decoration: InputDecoration(
            border: OutlineInputBorder(
              borderRadius:
                  BorderRadius.circular(screenAwareSize(3, 6, context)),
              gapPadding: 0,
            ),
            hintText: 'Enter Note'),
        onSaved: (value) {
          //store your value here
        },
        validator: (value) {
          if (value.isEmpty) {
            return 'Notes can\'t be empty';
          } else {
            return null;
          }
        });
  }

【问题讨论】:

  • 在检查 isEmpty 之前尝试修剪值(文本)。
  • 非常感谢 :) 我知道,我问了一个愚蠢的问题

标签: validation flutter dart textfield


【解决方案1】:

在检查之前修剪值。

validator: (value) {
  if (value.trim().isEmpty)
    return "Notes can't be empty";

  return null;
}

(Harsha Pulikollu 也建议将其作为评论,不知道为什么没有将其添加为答案)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    相关资源
    最近更新 更多