Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Address Validation for ASP.NET

What do you use to validate an email address on a ASP.NET form. I want to make sure that it contains no XSS exploits.

This is ASP.NET 1.1

like image 921
Brian G Avatar asked Oct 08 '08 12:10

Brian G


People also ask

Which validation is used for email in asp net?

This validator is used to validate the value of an input control against the pattern defined by a regular expression. It allows us to check and validate predictable sequences of characters like: e-mail address, telephone number etc.

Which validation is used to validate the email address?

Double opt-in is the best way to validate email addresses.


2 Answers

Any script tags posted on an ASP.NET web form will cause your site to throw and unhandled exception.

You can use a asp regex validator to confirm input, just ensure you wrap your code behind method with a if(IsValid) clause in case your javascript is bypassed. If your client javascript is bypassed and script tags are posted to your asp.net form, asp.net will throw a unhandled exception.

You can use something like:

<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="tbEmail" ErrorMessage="Invalid Email Format"></asp:RegularExpressionValidator> 
like image 161
WebDude Avatar answered Sep 19 '22 19:09

WebDude


Here is a basic email validator I just created based on Simon Johnson's idea. It just needs the extra functionality of DNS lookup being added if it is required.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls; using System.Text.RegularExpressions; using System.Web.UI;  namespace CompanyName.Library.Web.Controls {     [ToolboxData("<{0}:EmailValidator runat=server></{0}:EmailValidator>")]     public class EmailValidator : BaseValidator     {          protected override bool EvaluateIsValid()         {             string val = this.GetControlValidationValue(this.ControlToValidate);             string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";             Match match = Regex.Match(val.Trim(), pattern, RegexOptions.IgnoreCase);              if (match.Success)                 return true;             else                 return false;         }      } } 

Update: Please don't use the original Regex. Seek out a newer more complete sample.

like image 20
John_ Avatar answered Sep 20 '22 19:09

John_