Email Address Validation for ASP.NET

前端 未结 8 863
梦如初夏
梦如初夏 2020-12-01 13:45

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

8条回答
  •  佛祖请我去吃肉
    2020-12-01 14:11

    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>")]
        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.

提交回复
热议问题