仅允许来自特定电子邮件域的注册 [英] Only allow registrations from specific email domains
问题描述
我正在开发一个使用新MVC网站模板随附的内置帐户模型/控制器的网站.我希望只允许人们在他们的电子邮件地址中使用两个特定域之一的情况下进行注册.
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.
例如,他们可以使用@ domain1.co.uk或@ domain2.co.uk进行注册,但不能使用其他域(例如Gmail,Yahoo等).
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.
推荐答案
如果使用MVC3默认站点,则将有一个/Models/AccountModels.cs
文件.您可以在此处添加正则表达式以进行客户端*和服务器端验证.
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.
* 客户端验证假定您的视图引用了jquery.validate脚本,并且具有Html.ValidationMessageFor(m => m.Email)
和/或Html.ValidationSummary()
,默认情况下应为.
*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.
这篇关于仅允许来自特定电子邮件域的注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!