Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

angular - How to add validation in template driven form in ionic 3?

How to validate mobile number in ionic 3, here i have used ng model, is there any example for it to get done ?

    <ion-list>
        <ion-item margin-bottom>
            <ion-input type="number" id="phone"   name="phone"  [(ngModel)]="data.phone" #username="ngModel" placeholder="Mobile"></ion-input>
         
        </ion-item>



        <ion-item margin-bottom> 
            <ion-input type="password" id="password" name="password"  [(ngModel)]="data.password" placeholder="*Password"></ion-input>
        </ion-item>

        <div style="text-align:center;">
            <button ion-button color="primary" type="submit" (click)="login()">
        <ion-icon ios="ios-log-in" md="md-log-in"></ion-icon>
      Login</button>
        </div>
    </ion-list>
question from:https://stackoverflow.com/questions/65626649/how-to-add-validation-in-template-driven-form-in-ionic-3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.html file code

<ion-list>
    <ion-item margin-bottom>
        <ion-input type="number" id="phone"   name="phone"  [(ngModel)]="data.phone" #username="ngModel" placeholder="Mobile" (ionChange)='validateForm()'></ion-input> 
        <br/>
        {{phoneErrorMessage}}
    </ion-item>

    <ion-item margin-bottom> 
        <ion-input type="email" id="email" name="email"  [(ngModel)]="data.email" placeholder="*Password" (ionChange)='validateForm()'></ion-input>

        <br/>
        {{emailErrorMessage}}
    </ion-item>

    <div style="text-align:center;">
        <button ion-button color="primary" type="submit" (click)="login()">
            <ion-icon ios="ios-log-in" md="md-log-in"></ion-icon>Login
        </button>
    </div>
</ion-list>

.ts file code

public phoneErrorMessage = '';
  public emailErrorMessage = '';
  public data = {email:'',phone:''}
  validateForm(){
      var phoneValidationFlag = false ;
      var emailValidationFlag = false ;
  
      if (this.data.phone == "") {
          this.phoneErrorMessage = "Name must be filled out";
          phoneValidationFlag = false ; 
      }else{
          this.phoneErrorMessage = '';
          phoneValidationFlag = true;
      }
  
      var emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/;
      if (!emailRegex.test(this.data.email)){  
          this.emailErrorMessage =  'Please enter valid email';
          emailValidationFlag = false
      }else{
          this.emailErrorMessage = '';
          emailValidationFlag = true;
      }
  
      return (emailValidationFlag && phoneValidationFlag) ? true : false ;
  }
  
  
  login(){
      if(this.validateForm()){
          //everything validated and you can excute your api call in this if section
      }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...