HOW TO USE REGULAR EXPRESSION FOR EMAIL VALIDATION IN MVC

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 531  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. aditya kumar Patel

    Post:254

    Points:1810
    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 Feb-26-2016 12:32:34 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!