我在使用打字稿使用正则表达式验证电子邮件地址是否有效或无效时遇到问题,这就是我所拥有的。
我收到运行时错误"无法读取未定义的属性'值'"
public signup(email){
var reg = /^([w-.]+@(?!lsu.edu)([w-]+.)+[w-]{2,4})?$/
if (reg.test(email.value) == false) {
let alert = this.alertCtrl.create({
title: 'Error ',
subTitle: 'Invalid email.',
buttons:['Try Again']
});
alert.present();
return false;
}
}
完整代码
import {Component} from '@angular/core';
import { BackandService } from '@backand/angular2-sdk'
import { LoginPage } from '../login/login';
import { AlertController } from 'ionic-angular';
@Component({
templateUrl: 'signup.html',
selector: 'page-signup',
})
export class SignupPage {
email:string = '';
firstName:string = '';
lastName:string = '';
signUpPassword: string = '';
confirmPassword: string = '';
bloodType: Object = {};
constructor(private backand: BackandService, private alertCtrl:
AlertController) {
}
public signUp(email) {
if(this.email == '' || this.firstName == '' || this.lastName == '' || this.signUpPassword == '' || this.confirmPassword == ''){
let alert = this.alertCtrl.create({
title: 'Error ',
subTitle: 'Must fill in all fields.',
buttons:['Try Again']
});
alert.present();
}
if(this.signUpPassword != this.confirmPassword){
let alert = this.alertCtrl.create({
title: 'Error ',
subTitle: 'Passwords must match.',
buttons:['Try Again']
});
alert.present();
}
if(this.signUpPassword.length < 6 && this.confirmPassword.length < 6){
let alert = this.alertCtrl.create({
title: 'Error ',
subTitle: 'Password must be 6 characters.',
buttons:['Try Again']
});
alert.present();
}
var reg = /^([w-.]+@(?!lsu.edu)([w-]+.)+[w-]{2,4})?$/
if (reg.test(email.value) == false) {
let alert = this.alertCtrl.create({
title: 'Error ',
subTitle: 'Invalid email.',
buttons:['Try Again']
});
alert.present();
return false;
}
this.backand.signup(this.firstName, this.lastName, this.email, this.signUpPassword, this.confirmPassword, this.bloodType)
.then((res: any) =>
{
let alert = this.alertCtrl.create({
subTitle: 'Thank you for signing up.',
buttons:['Login']
});
alert.present();
this.email = this.signUpPassword = this.confirmPassword = this.firstName = this.lastName = this.bloodType = '';
}
);
}
}
验证电子邮件类型脚本&&validation mot de passe type script
validateData(field: any) {
const validateEmail = (email: any) => {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/
);
};
switch (field) {
case 'userName':
if (this.user.userName == '') {
this.errorMessages.userName = "l'adresse email ne peut pas être vide";
} else if (! validateEmail(this.user.userName)) {
this.errorMessages.userName = 'Adresse email non valide';
} else this.errorMessages.userName = '';
break;
case 'nom':
if (this.user.nom == '')
this.errorMessages.nom = 'le prénom ne peut pas être vide';
else this.errorMessages.nom = '';
break;
case 'prenom':
if (this.user.prenom == '')
this.errorMessages.prenom = 'le nom ne peut pas être vide';
else this.errorMessages.prenom = '';
break;
case 'userName':
if (this.user.userName == '')
this.errorMessages.userName =
"le nom d'utilisateur ne peut pas être vide";
else this.errorMessages.userName = '';
break;
case 'password2':
if (this.password2 == '')
this.errorMessages.password2 =
'confirmation mot de passe ne peut pas être vide';
else this.errorMessages.password2 = '';
break;
case 'password':
if (this.user.password == '')
this.errorMessages.password = 'le mot de passe ne peut pas être vide';
else if (!this.user.password.match(/[0-9]/g)) {
this.errorMessages.password =
'mot de passe doit contenir au minimum un chiffre';
} else if (!this.user.password.match(/[a-z]/g)) {
this.errorMessages.password =
'mot de passe doit contenir au minimum une lettre minuscule';
} else if (!this.user.password.match(/[A-Z]/g)) {
this.errorMessages.password =
'mot de passe doit contenir au minimum une lettre majuscule';
} else if (!(this.user.password.length >= 10)) {
this.errorMessages.password =
'mot de passe doit contenir au minimum 10 caractères';
} else this.errorMessages.password = '';
break;
default:
}
}