I am developing a site that uses the built in account model / controller that comes with the new MVC site template. I want to be able to only allow people to register if they use one of two specific domains in their email address.
So for example they can register if they use @domain1.co.uk or @domain2.co.uk, but no other domains (for example Gmail, Yahoo etc) can be used.
If anyone could point me in the right direction that would be great.
What more do you need than:
if( email.Contains("@domain1.co.uk") || email.Contains("@domain2.co.uk") )
Register(email);
else
throw, return false, whatever()
If using the MVC3 default site, you'll have a /Models/AccountModels.cs
file. You can add a regular expression there to cause client-side* and server-side validation.
public class RegisterModel
{
...
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[RegularExpression(@"^[a-zA-Z0-9._%+-]+(@domain1\.co\.uk|@domain2\.co\.uk)$", ErrorMessage = "Registration limited to domain1 and domain2.")]
public string Email { get; set; }
...
}
You will need to work out the expression that works out best for your requirements.
*client-side validation assumes your view references the jquery.validate script and has Html.ValidationMessageFor(m => m.Email)
and/or Html.ValidationSummary()
, which it should by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With