Javascript 如何使用Angular 5验证电子邮件

Javascript 如何使用Angular 5验证电子邮件,javascript,html,angular,typescript,Javascript,Html,Angular,Typescript,我想知道如何用.com验证我的电子邮件 我什么都试过了,但是不行,你能帮我吗? 我正在结束我已经开发的代码,我对这个角度是新手,我发现在这个问题上很难。 如果用户键入.com,我希望对电子邮件进行验证,但到目前为止,我只得到了基本的验证,例如:Example@Example。 我希望是这样example@example.com 多谢各位 .ts html 诺梅 requiredo 欢迎您发送电子邮件至valido 电子邮件 requiredo 我们的条件 埃内切萨里奥酒店 羡慕 试试这

我想知道如何用.com验证我的电子邮件 我什么都试过了,但是不行,你能帮我吗? 我正在结束我已经开发的代码,我对这个角度是新手,我发现在这个问题上很难。 如果用户键入.com,我希望对电子邮件进行验证,但到目前为止,我只得到了基本的验证,例如:Example@Example。 我希望是这样example@example.com

多谢各位

.ts

html


诺梅
requiredo
欢迎您发送电子邮件至valido
电子邮件
requiredo

我们的条件 埃内切萨里奥酒店
羡慕

试试这个,我们可以用正则表达式模式来验证

<input type="email" [(ngModel)]="enterEmail" name="myEmail" 
#myEmail="ngModel" email pattern="[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}" required>

您需要更改您的模式,请使用下面的模式作为电子邮件id

有效电子邮件ID的示例:

sample@sam.com-有效, sample@sam.in-有效, sample@sam-因弗利德

pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"
试试正则表达式

emailFormControl: new FormControl('', Validators.compose([Validators.required, Validators.pattern(/^(([^<>()\[\]\\.,;:\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,}))$/)])),
emailFormControl:new FormControl(“”,Validators.compose([Validators.required,Validators.pattern(/^([^()\[\]\\,;:\s@“]\[\]\\,;:\s@”]+*)(“+”)@(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]+]a-[1,3}.[1,3}.],
您可以像type=“email”一样使用HTML5 type属性
输入有效的电子邮件
或
使用模式属性,如:
pattern=“^[a-z0-9.\%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$”带有默认类型=“text”。

试试这个简单的正则表达式

if (emailString.match(/[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9A-Z](?:[a-z0-9A-Z]*[a-z0-9A-Z])?\.)+[a-z0-9A-Z](?:[a-z0-9A-Z]*[a-z0-9A-Z])?/)) {
    return null;
  } else {
    return { 'invalidEmailAddress': true };
  }

检查此Stackblitz示例

首选Angular提供的电子邮件验证程序

<input type="email" name="email" ngModel email>
<input type="email" name="email" ngModel email="true">
<input type="email" name="email" ngModel [email]="true">

参考:


  • 更新了我的正则表达式。您现在可以检查吗?src/app/app.component.ts(96,25)中的错误:错误TS1005:'('expected.src/app/app.component.ts(96,36):错误TS1005:'='expected.src/app/app.component.ts(96258):错误TS1005:“;”应为。日期:2018-10-03T13:27:03.313Z-哈希:0aa142bd0a2f8edcc4ec-时间:546在StackBlitz中附加了一个示例,希望它能帮助您。可能重复的
     Either you can user HTML5 type attribute as email like type="email"
                <div class="form-group">
                  <input class="form-control text-sm-x2 pl-5" placeholder="Email Id" type="email" name="email" [(ngModel)]="userCredentials.email"
                    [disabled]="disableInputs" required email #email="ngModel">
                  <span class="help-block" *ngIf="email?.dirty && email?.errors?.email" style="color:red">
                    <small>Enter a valid email</small>
                  </span>
                </div>
      OR
    use pattern attribute like:
    pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" with default type="text".
    
    if (emailString.match(/[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9A-Z](?:[a-z0-9A-Z]*[a-z0-9A-Z])?\.)+[a-z0-9A-Z](?:[a-z0-9A-Z]*[a-z0-9A-Z])?/)) {
        return null;
      } else {
        return { 'invalidEmailAddress': true };
      }
    
    <input type="email" name="email" ngModel email>
    <input type="email" name="email" ngModel email="true">
    <input type="email" name="email" ngModel [email]="true">