使用默认值验证输入
问题: 如何验证输入中设置的默认值? 我的意思是我想忽略默认值,如果用户没有插入任何不同的值,验证将提醒他没有插入任何东西。 型号:
public class ContactUsViewModel
{
[Required(ErrorMessage = "please enter full name")]
public string FullName { get; set; }
[Required(ErrorMessage = "enter email")]
[Email(ErrorMessage = "enter email")]
public string Email { get; set; }
[Required(ErrorMessage = "enter phonenumber")]
[Mobile(ErrorMessage = "enter phonenumber")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "please address the issue")]
public string Issue { get; set; }
public IDictionary<string, object> FullNameProperties
{
get
{
return new Dictionary<string, object>()
{
{"Value", "FullName"}
};
}
}
}
视图:
<div class="ui-contact-us-form">
@using (Html.BeginForm("Contact","Home",FormMethod.Post))
{
<ul>
<li>
<label for="FullName" class="form-ui-icon-name ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.FullName, Model.ContactUsViewModel.FullNameProperties)
@Html.ValidationMessageFor(x => x.ContactUsViewModel.FullName)
</div>
</li>
<li>
<label for="PhoneNumber" class="form-ui-icon-phone ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.PhoneNumber, Model.ContactUsViewModel.PhoneNumber)
</div>
</li>
<li>
<label for="Email" class="form-ui-icon-email ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.Email, Model.ContactUsViewModel.EmailProperties)
<input type="text" value="מייל" name="Email"/>
</div>
</li>
<li>
<label for="Issue" class="form-ui-icon-pen ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.Issue, Model.ContactUsViewModel.IssueProperties)
</div>
</li>
</ul>
<div class="spacer"></div>
<div class="ui-text-area">
@Html.TextAreaFor(x => x.ContactUsViewModel.Notes)
</div>
<div class="spacer"></div>
<div class="form-ui-btn">
<input type="submit" value="" />
</div>
}
</div>
Question:
How do i validate default values that set into the inputs?
I mean i want to ignore the default values and if the user haven't inserted any different value , the validation will alert him that he haven't inserted any thing.
Model:
public class ContactUsViewModel
{
[Required(ErrorMessage = "please enter full name")]
public string FullName { get; set; }
[Required(ErrorMessage = "enter email")]
[Email(ErrorMessage = "enter email")]
public string Email { get; set; }
[Required(ErrorMessage = "enter phonenumber")]
[Mobile(ErrorMessage = "enter phonenumber")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "please address the issue")]
public string Issue { get; set; }
public IDictionary<string, object> FullNameProperties
{
get
{
return new Dictionary<string, object>()
{
{"Value", "FullName"}
};
}
}
}
View:
<div class="ui-contact-us-form">
@using (Html.BeginForm("Contact","Home",FormMethod.Post))
{
<ul>
<li>
<label for="FullName" class="form-ui-icon-name ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.FullName, Model.ContactUsViewModel.FullNameProperties)
@Html.ValidationMessageFor(x => x.ContactUsViewModel.FullName)
</div>
</li>
<li>
<label for="PhoneNumber" class="form-ui-icon-phone ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.PhoneNumber, Model.ContactUsViewModel.PhoneNumber)
</div>
</li>
<li>
<label for="Email" class="form-ui-icon-email ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.Email, Model.ContactUsViewModel.EmailProperties)
<input type="text" value="מייל" name="Email"/>
</div>
</li>
<li>
<label for="Issue" class="form-ui-icon-pen ui-block-inline"></label>
<div class="input-black">
@Html.TextBoxFor(x => x.ContactUsViewModel.Issue, Model.ContactUsViewModel.IssueProperties)
</div>
</li>
</ul>
<div class="spacer"></div>
<div class="ui-text-area">
@Html.TextAreaFor(x => x.ContactUsViewModel.Notes)
</div>
<div class="spacer"></div>
<div class="form-ui-btn">
<input type="submit" value="" />
</div>
}
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)