asp.net mvc - Disable serverside MVC Validation if control is disabled -
is possible disable serverside mvc validation, if textbox disabled client-side javascript? because, if textbox disabled there won't content in it.
yes. possible. have reply you: remote validation. if utilize [required] attribute , dont fill field, modelstate respond not valid. alternative using remote validation attribute.
i create illustration you:
model:
[required] [regularexpression(@"^[0-9]*$", errormessage = "only numbers")] [remote("checkifisdisable", "home", additionalfields = "txtstatus", errormessage = "you must fill field")] [displayname("register number")] public string bookid { get; set; }
view:
@html.textboxfor(model => model.bookid) @html.validationmessagefor(model => model.bookid) @html.hidden("txtstatus", "enable") <input type="button" onclick = "return disabletxt()" text="toggle disable/enable bookid field"/>
you must set javascript function toggle value:
<script> function disabletxt() { if ($("#txtstatus").val() === "enable") { $("#txtstatus").val("disable"); } else { $("#txtstatus").val("enable"); } } </script>
controller:
public actionresult checkifisdisable(string bookid, string txtstatus) { if (txtstatus == "enable") { // show error message of validation homecoming json(true, jsonrequestbehavior.allowget); } else { // ignore validation homecoming json(false, jsonrequestbehavior.allowget); } }
is example. sense free alter , adapt needs. , sense free ask.
asp.net-mvc validation
No comments:
Post a Comment