ASP.NET MVC Validation
Hi all,
I have a quick question regarding MVC validation within MVC widgets. I've noticed that on-submit validation works beautifully - the form gets sent, fails to validate and I can show any relevant messages for the user to correct.
I was wondering if there's any support for the inline validation that MVC would normally allow - where each field is validated on the fly rather than on submit?
Hello Nick,
Along with your server-side validation, you can add a client-side validation by using for example JavaScript, where to add a violation message and/or to hide/deactivate the submit button (example).
Below is the sample code used in the View:
<script> function test() var name = document.getElementById('name').value; console.log(name.length); if (name.length >= 3) document.getElementById('err1').setAttribute("style", "display: none"); else document.getElementById('err1').setAttribute("style", "color:red; display: inline-block"); </script><form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post"> First name: <input type="text" name="fname" onchange="test()" id="name"> <span id="err1" style="display:none">Please enter at least 3 charecters</span> <br /> <input type="submit" value="Submit"></form>