Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中验证电子邮件#_C#_Asp.net Mvc - Fatal编程技术网

C# 如何在c中验证电子邮件#

C# 如何在c中验证电子邮件#,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试验证电子邮件字段。我有一个电子邮件的正则表达式 /^[a-z0-9.z%+-]+@[a-z0-9.-]+[a-z]{2,4}$/ 在VisualStudio中我的代码 Email:<br> <input type="text" ng-model="Email" id="txtEmail" ng-pattern="/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/" name="Email" placeholder=

我正在尝试验证电子邮件字段。我有一个电子邮件的正则表达式

/^[a-z0-9.z%+-]+@[a-z0-9.-]+[a-z]{2,4}$/

在VisualStudio中我的代码

  Email:<br>
        <input type="text" ng-model="Email" id="txtEmail" ng-pattern="/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/" name="Email" placeholder="Email" required>
        <span ng-show="myForm.Email.$error.pattern">Please enter valid Email!</span>
        <span ng-show="myForm.Email.$error.required">*</span><br>
电子邮件:
请输入有效的电子邮件! *
发生的错误
您可以为模式创建变量

@{
    var pattern = "/^[a-z0-9._%+-]+@@[a-z0-9.-]+.[a-z]{2,4}$/";
}

<input type="text" ng-model="Email" id="txtEmail" ng-pattern="@pattern" name="Email" placeholder="Email" required>
@{
var pattern=“/^[a-z0-9.z%+-]+@[a-z0-9.-]+[a-z]{2,4}$/”;
}

电子邮件验证是一个普遍存在的问题,您将发现的每个正则表达式模式并不涵盖所有情况。最好的方法是尝试将任何消息发送到此电子邮件,或者您可以使用System.Net.Mail.MailAddress类

        try
        {
            var email = new System.Net.Mail.MailAddress(value);
        }
        catch (FormatException)
        {
            //handle it here
        }
你说你在使用C和MVC。您可以使用内置的验证;通过模型类中字段上的属性支持电子邮件验证:

[EmailAddress(ErrorMessage = "The email address is not valid")]
public string Email { get; set; }
属性位于名称空间
System.ComponentModel.DataAnnotations

然后,Razor视图需要如下内容:

<div class="control-group">
    @Html.ValidationMessageFor(m => m.Email)
    @Html.LabelFor(m => m.Email, new { @class = "editor-label control-label" })
    <div class="editor-field controls">
        @Html.MbrraceTextBoxFor(m => m.Email, new { @class = "input-xlarge" })
     </div>
</div>

@Html.ValidationMessageFor(m=>m.Email)
@LabelFor(m=>m.Email,新的{@class=“editor label control label”})
@mbracetextboxfor(m=>m.Email,新的{@class=“input xlarge”})

cshtml
显示样式上有许多变体。

Itr很奇怪,你用剃刀视图来保存角度模板好主意;如何在客户端使用它。@PeterSmith您可以选择众所周知的验证库,例如jQuery验证,或者甚至为前端和后端构建自己的验证库(链接到核心,但概念是相同的)。