I cannot find any easy way of making borders green for fields that are valid in ASP.NET MVC. I am using DataAnnotations and Unobtrusive jquery validation. I have all the default validation styles and I can easily change border color of invalid controls and colour of the messages. However, there there is no style being added to valid fields. How can I do it. DO I need to hijack it with jquery?
Another question I have is how to trigger validation on fields on blur without writing much of the custom code? By default it validates only on form submit. Do I have to use Remote Validation for that? I was hoping there was an easy setting somewhere to enable it.
EDIT
<% using (Ajax.BeginForm("SaveSupplier",
"Suppiers",
new AjaxOptions
{
UpdateTargetId = "supplier-details-wrapper",
InsertionMode = InsertionMode.Replace
},
new { id = "supplier-details-form", enctype = "multipart/form-data" }))
{ %>
<div id="supplier-details">
<p>
<%: Model.PageDescription %>
</p>
<%: Html.AntiForgeryToken() %>
-- MY FIELDS HERE --
</div>
<% } %>
<div class="form-buttons">
<button id="save-button" type="button" class="submit-button">Save</button>
<button id="cancel-button" type="button" class="submit-button">Cancel</button>
</div>
ANd javascript
<script type="text/javascript">
$(function () {
$("#save-button").click(function () {
$("#supplier-details-form").submit();
});
$('input[data-val=true]').on('blur', function () {
$(this).valid();
});
});
</script>
I also have
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>"></script>
<script src="<%: Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") %>"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"> </script>
And in web.config
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
My issue is that it only does validation on server side no matter what I do. How do I manually trigger it in javascript. I tried various things and it always does postback to the server. $("#my-element-id").valid() sort of works, it returns 1 or 0, but it doesnt highlight text box and doesnt show error. Validate doesnt seem to do anything. I have a suspicion it has something to do with my buttons being outside of Ajax form or because of AntiFormgeryToken.