当扑扑的otp字段为空时如何向otp提交验证

时间:2019-09-07 11:18:55

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

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

enter image description here

  _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();
  }

1 个答案:

答案 0 :(得分:3)

为了验证空字符串,您只需要检查其值是否等于“”即可。

您可以使用吸气剂功能isEmpty()来检查字符串是否为空。

解决方案代码:

 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;
                        }
                      },