I want to be able to initiate client-side form validation when the user selects the GO button. Currently, when the GO button is selected, it does not check the full form. For example, if I download a form and select the Go button, without focusing on the text field, nothing is checked, and val.Valid () returns true. If I enter incorrect data in a text box, validation is performed for this single element; and when I select the "GO" button, val.Valid () returns false.
So what I'm trying to do is when the user selects a button or other event, I want to start checking all forms.
I do it in MVC 3
public class TestValidationModel { [Required(ErrorMessage="UserName Is Required")] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [StringLength(12, MinimumLength = 3)] public string UserName { get; set; } [Required(ErrorMessage="Password Is Required")] [StringLength(20, MinimumLength = 3)] public string Password { get; set; } [Required(ErrorMessage="Email Address Is Required")] [Display(Name = "Email Address")] public string EmailAddress{ get; set; } } <script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script> <script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script> <script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script> <script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#butValidateForm").click(function(){ var val = $('#myForm').validate() alert(val.valid()); }) }); </script> @using (Html.BeginForm("", "", FormMethod.Post, new {id = "myForm"})) { <div> @Html.LabelFor(m => m.UserName) @Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName) </div> <div> @Html.LabelFor(m => m.Password) @Html.TextBoxFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password) </div> <div> @Html.LabelFor(m => m.EmailAddress) @Html.TextBoxFor(m => m.EmailAddress) @Html.ValidationMessageFor(m => m.EmailAddress) </div> <button id="butValidateForm">GO</button> <input type="submit" id="submitForm" value="Submit" /> }
Update
I think I found a solution to my problem. See ShowErrors below
<script type="text/javascript"> $(function () { $("#butValidateForm").click(function () { var val = $('#myForm').validate(); val.showErrors(); </script>
This next link to a post in jQuery forms did not address my problem, but I was able to find the names of the methods I was looking for. http://plugins.jquery.com/content/jqueryvalidate-showerrors-issue-form-wizard
If anyone has a better answer, please provide feedback.
Update
The previous code works somewhat in Firefox, but not at all in IE. I did the following and now it works in Firefox, but still not in ID
$(function () { $("#myForm").submit(function (e) { var val = $('#myForm').validate();
Thanks for using jQuery to capture ASP.NET MVC Form messages to point me in the right direction. But still not perfect
jquery jquery-validate asp.net-mvc-3
Mike Barlow - BarDev Feb 19 '11 at 23:59 2011-02-19 23:59
source share