网站搜索

angular - 如何在 ionic v3 中设置 AlertBox 输入的限制


所以我正在创建一个警告框,并且它有一些输入,我想限制该输入最多为10个字符,并确保只输入数字

我找不到任何关于那方面的指南,任何帮助都会感激不尽

>  const alert = this.alertCtrl.create({
                                            title: 'Please Enter your Mobile',
                                            inputs: [
                                            {
                                                name: 'mobile',
                                                placeholder: 'Mobile'
                                            }

                                            ],
                                            buttons: [
                                            {
                                                text: 'Done',
                                                handler: data => {
                                                            if (data.mobile != "" || data.mobile != null) {
                                                                    this.data3 = data.mobile;
                                                                    this.storage.set('number',data.mobile);

                                                            } 
                                                            else {
                                                                    this.ionViewDidLoad();
                                                            }
                                                }
                                            }
                                            ]
                                });
                                alert.present();

这是我的代码,我想限制输入字段 mobile 为 10 个字符,并且只接受数字值,同时是否可以检查它是否为空

答案#1

您可以在这里使用Toast通知。

警告框的 done 处理器:

{
          text: 'Done',
          handler: (data) => {
            if (MyValidator.isValid(data.mobile)) {
              if (this.data) {
                //my code
              } else {
                //my code
              }
              return true;
            } else {
              this.showErrorToast('Invalid mobile');
              return false;
            }
          }
        }

注意:这里 MyValidator 是一个验证类,您可以根据您的使用情况使用它。

Toast 方法是这样的:

showErrorToast(data: any) {
    let toast = this.toastCtrl.create({
      message: data,
      duration: 3000,
      position: 'top'
    });

    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }

你可以查看此处以获取更多信息。