关于javascript:如何在typescript中使用正则表达式进行电子邮件验证

How to do Email validation using Regular expression in Typescript

本问题已经有最佳答案,请猛点这里访问。

我需要使用typescript angular应用程序验证Excel文件中的电子邮件和日期字段。

我尝试使用正则表达式进行验证,但是对于正确的电子邮件地址,结果总是返回false。

有人能帮我验证电子邮件和日期吗?

下面是我写的代码

组件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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");
            }
        );
    }
}


给定的regex格式有问题。像这样不加引号(")地给出它

1
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,}))$/);