c# - How Can I Validate that an Email is from a Particular Domain in ASP.NET MVC? -



c# - How Can I Validate that an Email is from a Particular Domain in ASP.NET MVC? -

i building application rolled out specific college campuses.

i ensure that, when user signs up, , provides email address, school provided email ensure legitimate students have access service.

does have illustration or ideas on how accomplish this? perhaps custom validation attribute?

note: more specifically, need ensure email come in .edu email address

looks regex way go...can provide guidance on proper expression?

i create regular look attribute , custom dataannotation. register annotation under application_start in global. can utilize validation dataannotation in model both client , server side. have regularexpressattributes.cs class contains commonly used regular expressions; drop projects. if folks want allow me know.

the view:

<div class="editor-field span-7"> @html.labelfor(model => model.emailaddress) @html.editorfor(model => model.emailaddress) @html.validationmessagefor(model => model.emailaddress) </div>

regular look attribute

using system; using system.collections.generic; using system.web; using system.componentmodel.dataannotations; using system.web.mvc; namespace projectsmvc.helpers { #region regularexpressionattributes /// <summary> /// email validation regular look attribute /// </summary> /// <remarks>validates person@someplace.com, some.person@someplace.com, some_person@some+place.com , combinations thereof.</remarks> public class validateemailattribute : regularexpressionattribute { // public validateemailattribute() // : base(@"^\s?([0-9a-za-z]([-.\w]*[0-9a-za-z])*@([0-9a-za-z][-\w]*[0-9a-za-z]\.)+[a-za-z]{2,9})$") { } public validateemailattribute() : base(@)@"^\s?([0-9a-za-z]([-.\w]*[0-9a-za-z])*@someplace.com$") {} } #region dataannotationsmodelvalidator public class emailvalidator : dataannotationsmodelvalidator<validateemailattribute> { #region properties /// <summary> /// error message /// </summary> private readonly string _errormessage; /// <summary> /// regular look pattern /// </summary> private readonly string _pattern; #endregion #region constructors /// <summary> /// initializes new instance of <see cref="emailvalidator"/> class. /// </summary> /// <param name="metadata">the meta data.</param> /// <param name="context">the context.</param> /// <param name="attribute">the attribute.</param> public emailvalidator(modelmetadata metadata, controllercontext context, validateemailattribute attribute) : base(metadata, context, attribute) { this._errormessage = attribute.errormessage; this._pattern = attribute.pattern; } #endregion #region methods /// <summary> /// retrieves collection of client validation rules. /// </summary> /// <returns>a collection of client validation rules.</returns> public override ienumerable<modelclientvalidationrule> getclientvalidationrules() { var rule = new modelclientvalidationregexrule(this._errormessage, this._pattern); homecoming new[] { rule }; } #endregion } }

global.ascx.cs

protected void application_start() { arearegistration.registerallareas(); registerglobalfilters(globalfilters.filters); registerroutes(routetable.routes); // register custom model validators dataannotationsmodelvalidatorprovider.registeradapter(typeof(validateemailattribute), typeof(emailvalidator)); }

lastly model, user.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.componentmodel.dataannotations; using projectsmvc.helpers; namespace projectsmvc.models { [metadatatype(typeof(user_validation))] public partial class user { public string propername { { homecoming string.format("{0} {1}", this.firstname, this.lastname); } } public string directoryname { { homecoming string.format("{0}, {1}", this.lastname, this.firstname); } } public string isuseractive { { homecoming dictionaries.trueorfalse.first(t => t.key == this.isactive).value.tostring(); } } } public class user_validation { [display(name = "ename")] [required(errormessage = "required")] [validateename(errormessage = "invalid")] public string username { get; set; } [display(name = "first directoryname")] [required(errormessage = "required")] public string firstname { get; set; } [display(name = "last directoryname")] [required(errormessage = "required")] public string lastname { get; set; } [display(name = "email address")] [required(errormessage = "required")] [validateemail(errormessage = "invalid")] public string emailaddress { get; set; } [display(name = "active user")] [required(errormessage = "required")] public bool isactive { get; set; } } }

c# asp.net .net asp.net-mvc asp.net-mvc-3

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -