【问题标题】:How can i add validation to otp filed when otp field is empty in flutter当 otp 字段在颤振中为空时,如何向 otp 文件添加验证
【发布时间】:2020-01-09 23:47:42
【问题描述】:

在这里我遇到了一些问题。如何向纯文本字段或用于 OTP 输入的任何字段添加验证。如果有人有另一个 OTP 输入字段,请告诉我。希望你能理解。请帮帮我。

  _onAlertotp(context) {
    Alert(
        context: context,
        title: "OTP expires in: MM:SS (Counter",
        desc:
            "We have Texted and/or Emailed OTP (One Time Pin) to your registered cell phone and/ or email account. Please check and enter OTP below to activate your TUDO account",
        content: Column(
          children: <Widget>[
            Form(
              autovalidate: true,
              child: PinField(
                  padding: EdgeInsets.all(16.0),
                  length: 6,
                  gap: 6.0,
                  onSubmitted: Validators().otpValidate,
                  keyboardType: TextInputType.numberWithOptions(signed: true),
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly]),
            )
          ],
        ),
        buttons: [
          DialogButton(
            onPressed: () => Navigator.pop(context),
            child: Text(
              "Resend OTP",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          ),
          DialogButton(
            onPressed: () {
              _onAlertrunningbusiness(context);
            },
            child: Text(
              "Enter Otp",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          )
        ]).show();
  }

【问题讨论】:

  • 该字段没有验证器属性

标签: validation flutter textfield flutter-layout one-time-password


【解决方案1】:

为了验证一个空字符串,您只需要检查它的值是否等于“”。

您可以使用getter函数isEmpty()来检查String是否为空。

解决方案代码:

 validator: (String value) {
                        if (value.isEmpty) {
                          return "Please enter phone number";
                        } else if (value.length != 10) {
                          return "Please enter valid Phone number";
                        } else {
                          return null;
                        }
                      },

【讨论】:

  • 不,我们在 thr pin 文本字段中没有验证器属性。建议点别的。谢谢
猜你喜欢
  • 2020-12-08
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 2022-01-25
  • 2020-04-01
  • 2021-06-16
  • 2022-12-28
相关资源
最近更新 更多