我將客戶端驗證應用於我的表單中的字段,以便它們不保留爲空。但是,該消息顯示在輸入字段的下面。我怎樣才能讓它們出現在場地旁邊?爲mvc表單字段定位驗證消息
表單字段:
@using (Html.BeginForm("SaveAccount", "RxCard", FormMethod.Post, new { id = "save", enctype = "multipart/form-data" }))
{
<label id="lblAccountName">Account Name</label>
@Html.TextBoxFor(model => model.Pharmacy.AccountName, new { @id = "txtAccountName", @Name = "txtAccountName", required = "required" })
...
}
的Jquery:
$(document).ready(function() {
$('#save').validate({
onchange: function (element) {
this.element(element);
console.log('onchange fired');
},
onfocusout: function (element) {
this.element(element);
console.log('onfocusout fired');
}
});
});
結果:
您使用HTML-5的驗證是由您的瀏覽器控制器。使用MVC的內置驗證功能(使用驗證屬性和@ Html.ValidationMessageFor())來獲得客戶端和服務器端驗證,並允許您定位/設置消息的樣式。注意:不要試圖用'@Name =「txtAccountName」'覆蓋'name'屬性 - 它保證模型綁定將失敗 –
您正在使用哪種css進行設計? –
@StephenMuecke的事情是,我需要驗證發生onchange事件。這就是爲什麼我沒有在我的模型中使用驗證屬性。有沒有一種方法來調整驗證信息,以便在onchange上發生? – thatdude