2
votes

is there any possibility to validate a form inside prompt alert, similar to a form validation in a page. We use ngModel for validation in a page but i dont know how to validate a form inside a prompt

alert(){
        console.log("hi");
        let prompt = this.alertCtrl.create({
            title: 'Login',
            inputs: [
                {
                    name: 'username',
                    placeholder: 'User Name'
                },
                {
                    name: 'phone',
                    placeholder: 'Phone Number'
                }

            ],

            buttons: [
                {
                    text: 'Cancel',
                    handler: data => {
                        console.log('Cancel clicked');
                    }
                },

                {
                    text: 'Save',
                    handler: data => {
                        console.log('Saved clicked',data);
                        this.User_data = data;
                    }
                }
            ]
        });
        prompt.present();
    }

setting min and max value to input field and stating it as 'required', user name should not have numbers, phone number count should be '9'.

1

1 Answers

0
votes

I don't think that it's possible to validate an input inside an alert in Ionic2.

I had the same problem and worked around it by validating the input inside my save handler function:

    //...
    {
        text: 'Save',
        handler: data => {
            if(/* check if input is valid */){
               this.User_data = data;
            }else{
              // The input is not valid, alert the user or whatever
              return false;
            }
        }
    }
    //...