Home > DeveloperSection > Forums > How to use regular expression for email validation in mvc
Ankit Singh

Total Post:341

Points:2389
Posted on    February-26-2016 12:20 AM

 ASP.NET MVC Mvc  Data Validation 
Ratings:


 1 Reply(s)
 245  View(s)
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.


aditya kumar Patel

Total Post:246

Points:1752
Posted on    February-26-2016 12:22 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick