Javascript 如何使用 Typescript 中的正则表达式进行电子邮件验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46370725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do Email validation using Regular expression in Typescript
提问by user3301440
I have an requirement to validate email and date fields from an Excel file using typescript Angular app.
我需要使用 typescript Angular 应用程序验证 Excel 文件中的电子邮件和日期字段。
And I am trying to validate using regular expression but the result returns always false for a correct email address.
我正在尝试使用正则表达式进行验证,但对于正确的电子邮件地址,结果始终返回 false。
Can anyone help me to validate the email and dates?
谁能帮我验证电子邮件和日期?
Below is the code I have written
下面是我写的代码
Component:
成分:
import { Component } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
import {UploadService} from '../services//upload.service';
import { FileUploader ,FileItem,ParsedResponseHeaders,FileLikeObject} from 'ng2-file-upload';
import { SpotCheck } from '../models/SpotCheckFields';
@Component ({
selector: 'my-app',
templateUrl:'./excelUpload.html',
providers:[UploadService]
})
export class ExcelUploadComponent {
public SpotChecklist: SpotCheck[];
public project_master:any[];
uploader:FileUploader;
constructor(private uploadservice: UploadService ){
this.SpotChecklist=[];
this.project_master=[];
}
ngOnInit(): void {
this.uploader = new FileUploader({
url: 'http://localhost:5000/upload'
// headers: [{name:'Accept', value:'application/json'}],
// autoUpload: true,
});
this.uploader.onErrorItem = (item, response, status, headers) => this.onErrorItem(item, response, status, headers);
this.uploader.onSuccessItem = (item, response, status, headers) => this.onSuccessItem(item, response, status, headers);
// retrieve projectmaster details
this.getProjectMaster("","SELECT PROJECT MASTER");
}
onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
//console.log("onSuccessItem " + status, response, item);
this.SpotChecklist = JSON.parse(response); //success server response
var data = this.validateRow(this.SpotChecklist);
console.log(data);
}
onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
let error = JSON.parse(response); //error server response
}
validateRow(lst:any[]) : SpotCheck[]
{
var i:number;
for(i=0;i<lst.length ;i++)
{
var validation_message:string="";
var blnErrOccured:boolean=false;
if(!this.isEmail(lst[i].RESPONSIBLE_PERSON_EMAIL_ID))
{
validation_message=validation_message+ "," +"RESPONSIBLE_PERSON_EMAIL_ID is invalid"
blnErrOccured=true;
}
lst[i].VALIDATION_RESULT=validation_message;
}
return lst;
}
isDate(date:string) {
// return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}
isEmail(search:string):boolean
{
var serchfind:boolean;
regexp = new RegExp('/^(([^<>()\[\]\.,;:\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,}))$/');
serchfind = regexp.test(search);
console.log(serchfind)
return serchfind
}
getProjectMaster(project_code:string,Flag:string):any
{
this.uploadservice.getProjectMaster(project_code,Flag).subscribe(
response=> {
this.project_master= response[0];
return response;
},
error=> {
console.log("ERROR: ",error);
console.log(error.json()); //gives the object object
},
() => {
console.log("Completed");
}
);
}
}
采纳答案by prabushitha
Problem is with the regex format given. Give it without quotes (') like this
问题在于给出的正则表达式格式。像这样不带引号 (') 给它
regexp = new RegExp(/^(([^<>()\[\]\.,;:\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,}))$/);

