我有一个文本框,想要在提交表单时显示该字段。如果我单击文本框和选项卡以转到下一个文本框,则会出现错误消息。我有'要求''我的模型上的数据注释如下:
[DisplayName("Shipment Number")]
[Required]
public string ShipmentNumber { get; set; }
我的观点如下:
<div class="col-sm-8">
@Html.TextBoxFor(model => model.ShipmentNumber, new {@class = "form-control", @maxlength = "8", @title = "Maximum length is 8"})
@Html.ValidationMessageFor(model => model.ShipmentNumber)
</div>
哦,我还在$ function中添加了这些:
$("form").kendoValidator();
$.validator.setDefaults({
ignore: ""
});
当我将文本框留空并提交表单时,表单会在未显示错误消息的情况下提交,即“需要出货编号!”#39;。 P.S:我也试过DataAnnotation:
[Required(ErrorMessage = "ShipmentNumber Is Required")]
有谁知道我怎么能做到这一点?
更新 这解决了它: Razor代码的更改以使文本框成为必需并添加了必需的错误消息。
@Html.TextBoxFor(model => model.ShipmentNumber, new {@class = "form-control", @maxlength = "8", @title = "Maximum length is 8", data_required_msg = "Shipment Number is required!", required = "required" })
在javascript中的按钮提交代码中,执行了以下操作:
var validator = $(".form-horizontal").kendoValidator().data("kendoValidator");
if (validator.validate()) {
//code that needs to be executed if it is valid
}
注意:我还从模态类中删除了数据注释,@ Html.ValidationMessageFor和$(&#34; form&#34;)。kendoValidator代码。
答案 0 :(得分:1)
单击按钮时尝试强制验证:
// Validate the input when the Save button is clicked
$("#save").on("click", function() {
if (validator.validate()) {
// If the form is valid, the Validator will return true
save();
}
});
或
$("form").submit(function(event) {
event.preventDefault();
if (validator.validate()) {
status.text("Hooray! Your tickets has been booked!")
.removeClass("invalid")
.addClass("valid");
} else {
status.text("Oops! There is invalid data in the form.")
.removeClass("valid")
.addClass("invalid");
}
});
请参阅http://docs.telerik.com/kendo-ui/framework/validator/overview