首页 > 解决方案 > 当 otp 字段在颤振中为空时,如何向 otp 文件添加验证

问题描述

在这里我遇到了一些问题。如何向纯文本字段或用于 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();
  }

标签: validationfluttertextfieldflutter-layoutone-time-password

解决方案


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

您可以使用 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;
                        }
                      },

推荐阅读