如何在 ionic 3 中以模板驱动形式添加验证?
How to add validation in template driven form in ionic 3?
如何在 ionic 3 中验证手机号码,这里我使用了 ng 模型,是否有任何示例可以完成?
<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>
.html 文件代码
<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文件代码
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
}
}
如何在 ionic 3 中验证手机号码,这里我使用了 ng 模型,是否有任何示例可以完成?
<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>
.html 文件代码
<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文件代码
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
}
}