How to use regular expression for email validation in mvc

Total Post:341

Points:2389
 829  View(s)
Ratings:
Rate this:
We want to  use regular expression for email validation in mvc .
I have already used  "^[a-zA-Z0-9-.]+@[a-zA-Z0-9-.]+$"  but it is not working  with  "_"  characters .
How we will do this Please help me.
  1. Post:281

    Points:2003
    Re: How to use regular expression for email validation in mvc

    If you are using pattern "^[a-zA-Z0-9-.]+@[a-zA-Z0-9-.]+$" please

    replace your pattern "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

    it will work properly.

    @model ForumMVC.Models.Customer
    
    @{
        ViewBag.Title = "CheckRegularExpression";
    }
    <script src="~/Scripts/jquery-2.1.4.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <h2>CheckRegularExpression</h2>
    @using (Html.BeginForm())
    {
        @Html.LabelFor(m => m.EmailId)
        @Html.TextBoxFor(m => m.EmailId)
        @Html.ValidationMessageFor(m => m.EmailId)
    }


    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Linq.Expressions;

    using System.Web;

    using System.ComponentModel.DataAnnotations;

    namespace ForumMVC.Models

    {

    public class Customer

    {

    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",

    ErrorMessage = "Please enter a valid e-mail-2 adress")]

    public string EmailId

    {

    get;

    set;

    }

    }

    }

     

      Modified On Apr-11-2018 01:56:24 AM

Answer