---
title: "How to C# and javascript email validation regex"  
description: "How to C# and javascript email validation regex"  
author: "Norman Reedus"  
published: 2015-01-18  
updated: 2015-01-18  
canonical: https://www.mindstick.com/forum/12869/how-to-c-sharp-and-javascript-email-validation-regex  
category: "asp.net"  
tags: ["c#", "javascript", "regex"]  
reading_time: 1 minute  

---

# How to C# and javascript email validation regex

I have [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) and I need to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) is [email](https://www.mindstick.com/articles/12870/10-easy-ways-to-manage-your-business-email-inbox) correct or not. I always using that [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line), but it fails with asp.net, because @ is bad symbol.

```
function emailvalidate(e){    var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;    return b.test(e);}
```

How can I [fix](https://yourviews.mindstick.com/view/80763/donald-trump-ki-jeet-fix-hai) this? And its important to check email by [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript), not ASP.NET or C#.

P.S. Im really new with ASP.NET and JavaScript RegEx.

## Replies

### Reply by David Miller

Are you using the Razor view engine? If so you will need to escape those @ symbols if you are adding this script to the cshtml file itself

```
function emailvalidate(e){    var b=/^[^@@\s]+@@[^@@\.\s]+(\.[^@@\.\s]+)+$/;    return b.test(e);}
```

or you can wrap the regular expression in @Html.Raw as deherch mentioned:

var b = @Html.Raw(@"/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/");

you can also add this function to a script file with all your other common JS functions. This way you do not need to escape the razor syntax at all.

Win mentions that you want to use server-side validation. I would say best to use both. Client-side validation can stop it from ever hitting the server in the first place. And server-side validation because you can never trust what a client passes in.


---

Original Source: https://www.mindstick.com/forum/12869/how-to-c-sharp-and-javascript-email-validation-regex

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
