繁体   English   中英

如何在Ionic V3中的AlertBox中设置输入限制

[英]How to set the limit of input in AlertBox in ionic v3

所以我正在创建一个警报框,它有一些输入,并且我想将该输入限制为最多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();

这是我的代码,我想将输入字段限制为10个字符,并使其仅采用数字值,还可以检查其是否为空

您可以在此处使用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();
  }

您可以查看以获取更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM