Javascript 使用正则表达式进行typescript(ionic2)电子邮件验证

Javascript 使用正则表达式进行typescript(ionic2)电子邮件验证,javascript,regex,typescript,ionic2,email-validation,Javascript,Regex,Typescript,Ionic2,Email Validation,我在使用Typescript验证电子邮件地址是否有效时遇到了问题,使用正则表达式就是这样。 我遇到运行时错误“无法读取未定义的属性'value'” 完整代码 import {Component} from '@angular/core'; import { BackandService } from '@backand/angular2-sdk' import { LoginPage } from '../login/login'; import { AlertController } from

我在使用Typescript验证电子邮件地址是否有效时遇到了问题,使用正则表达式就是这样。
我遇到运行时错误“无法读取未定义的属性'value'”

完整代码

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 = '';
  }
);
  }
}
从'@angular/core'导入{Component};
从'@backand/angular2 sdk'导入{BackandService}
从“../login/login”导入{LoginPage};
从'ionic angular'导入{AlertController};
@组成部分({
templateUrl:'signup.html',
选择器:“页面注册”,
})
导出类签名{
电子邮件:string='';
firstName:string='';
lastName:string='';
signUpPassword:string='';
confirmPassword:字符串=“”;
血型:对象={};
构造函数(专用backand:BackandService,专用alertCtrl:
警报控制器){
}
公开注册(电子邮件){
如果(this.email=''this.firstName=''this.lastName=''this.signUpPassword=''this.confirmPassword=''){
让alert=this.alertCtrl.create({
标题:“错误”,
副标题:“必须填写所有字段。”,
按钮:[“重试”]
});
alert.present();
}
if(this.signUpPassword!=this.confirmPassword){
让alert=this.alertCtrl.create({
标题:“错误”,
副标题:“密码必须匹配。”,
按钮:[“重试”]
});
alert.present();
}
if(this.signUpPassword.length<6&&this.confirmPassword.length<6){
让alert=this.alertCtrl.create({
标题:“错误”,
副标题:“密码必须为6个字符。”,
按钮:[“重试”]
});
alert.present();
}
var reg=/^([\w-\.]+@(?!lsu.edu)([\w-]+\.+[\w-]{2,4})$/
if(注册测试(email.value)==false){
让alert=this.alertCtrl.create({
标题:“错误”,
副标题:“无效电子邮件”,
按钮:[“重试”]
});
alert.present();
返回false;
}
this.backand.signup(this.firstName、this.lastName、this.email、this.signUpPassword、this.confirmPassword、this.bloodType)
。然后((res:any)=>
{
让alert=this.alertCtrl.create({
副标题:“感谢您的注册。”,
按钮:[“登录”]
});
alert.present();
this.email=this.signUpPassword=this.confirmPassword=this.firstName=this.lastName=this.bloodType='';
}
);
}
}

您遇到了什么问题?我遇到了一个运行时错误“无法读取未定义的属性‘值’”。这意味着您的
电子邮件
未定义。这个错误与正则表达式无关。问题在于代码调用
signup()
。您的正则表达式匹配的
--@
不是有效的电子邮件地址。它抱怨
email
参数
未定义(因此您无法调用
email.value
)。解释调用函数的方式和位置。
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 = '';
  }
);
  }
}