[英]Append domain name to email for editorFor
I am using C#, MVC4 to build an account class that has a property called GmailAccount, which will be entered by the user. 我正在使用C#,MVC4来构建一个具有名为GmailAccount的属性的帐户类,该属性将由用户输入。
In Model, I am using the following RegularExpression attribute: 在模型中,我使用以下RegularExpression属性:
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@gmail.com"]
This validates the email ends in @gmail.com
. 这将验证电子邮件以
@gmail.com
结尾。
In my html view, I am simply using: 在我的html视图中,我只是在使用:
editorFor(model => model.GmailAccount) @@gmail.com
Note that I have typed in @@gmail in the html to save the user from typing in the domain name, and to ensure/enforce that user uses a valid gmail account. 请注意,我已经在html中键入@@ gmail,以免用户输入域名,并确保/强制用户使用有效的gmail帐户。
However, we also note that the same RegularExpression attribute is validated on both the client and the server side. 但是,我们还注意到,客户端和服务器端都验证了相同的RegularExpression属性。 I really don't want my user to type in the domain name manually since I won't allow it to be different from "@gmail.com".
我真的不希望我的用户手动输入域名,因为我不允许它与“ @ gmail.com”不同。
Question: 题:
Is there a way to append @gmail.com to whatever the user provided in editorFor as the user name before both client side and server side validation? 在客户端和服务器端验证之前,是否可以将@ gmail.com追加到editorFor提供的用户作为用户名? Or some other good alternatives?
还是其他一些好的选择?
What I would do is create a view model around what you are trying to do: 我要做的是围绕您要尝试执行的操作创建一个视图模型:
public EmailEntryViewModel
{
private string _emailPrefix = string.Empty;
public string FullEmailAddress
{
get
{
return _emailPrefix + "@gmail.com";
}
set
{
if (!value.EndsWith("@gmail.com", StringComparison.OrdinalIgnoreCase))
{
//whatever logic that is internal
throw new InvalidOperationException("Database contains an invalid email.");
}
this._emailPrefix = value.Substring(0,
email.IndexOf("@gmail.com", StringComparison.OrdinalIgnoreCase));
}
}
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"]
public string EmailPrefix
{
get
{
return _emailPrefix;
}
set
{
_emailPrefix = value;
}
}
}
Then you only need to: 然后,您只需要:
@Html.EditorFor(model => model.EmailPrefix) @@gmail.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.