Match two fields with jQuery validate plugin
You could use the equalTo method: $(‘#myform’).validate({ rules: { email: ‘required’, emailConfirm: { equalTo: ‘#email’ } } });
You could use the equalTo method: $(‘#myform’).validate({ rules: { email: ‘required’, emailConfirm: { equalTo: ‘#email’ } } });
var validator = $(“form”).validate() validator.errorList will show the array of errors that are holding back the form from submiting.
For the purposes of my example, this is the base starting code: HTML: <input type=”text” name=”field_1″ /> <input type=”text” name=”field_2″ /> <input type=”text” name=”field_3″ /> jQuery: $(‘#myForm’).validate({ rules: { field_1: { required: true, number: true }, field_2: { required: true, number: true }, field_3: { required: true, number: true } } }); http://jsfiddle.net/9W3F7 Option 1a) … Read more
Quote OP: “I have to select the datepicker twice. ie; 1 click to select and it inputs the data correctly. 2nd click to clear the error message.” That’s because validation is normally triggered on keyup and blur events. Since you’re using a date-picker to interact with the field, rather than the keyboard, you’re interfering with … Read more
Model [Required(ErrorMessage = “You must provide a phone number”)] [Display(Name = “Home Phone”)] [DataType(DataType.PhoneNumber)] [RegularExpression(@”^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$”, ErrorMessage = “Not a valid phone number”)] public string PhoneNumber { get; set; } View: @Html.LabelFor(model => model.PhoneNumber) @Html.EditorFor(model => model.PhoneNumber) @Html.ValidationMessageFor(model => model.PhoneNumber)
A nice way of handling this if you’re using Bootstrap 2 is… Add this to your _Layout.cshtml: <script type=”text/javascript”> jQuery.validator.setDefaults({ highlight: function (element, errorClass, validClass) { if (element.type === ‘radio’) { this.findByName(element.name).addClass(errorClass).removeClass(validClass); } else { $(element).addClass(errorClass).removeClass(validClass); $(element).closest(‘.control-group’).removeClass(‘success’).addClass(‘error’); } }, unhighlight: function (element, errorClass, validClass) { if (element.type === ‘radio’) { this.findByName(element.name).removeClass(errorClass).addClass(validClass); } else { $(element).removeClass(errorClass).addClass(validClass); … Read more
The novalidate attribute is added by jquery.validate line 32: // Add novalidate tag if HTML5. this.attr(‘novalidate’, ‘novalidate’); If you are using HTML5, then remove the attribute: $(“#contactForm”).validate(); $(“#contactForm”).removeAttr(“novalidate”); In your web.config, make sure you have: <appSettings> … <add key=”ClientValidationEnabled” value=”true” /> <add key=”UnobtrusiveJavaScriptEnabled” value=”true” /> </appSettings> Also make sure they are not commented out.
I think you either need to move back to an earlier version of jquery (1.5.2) or use the newer version of the validation plugin 1.8.0.1.
Here’s one way to proceed (there are other ways, I am just illustrating one that would match your view model as is): [AttributeUsage(AttributeTargets.Property)] public class RequireAtLeastOneOfGroupAttribute: ValidationAttribute, IClientValidatable { public RequireAtLeastOneOfGroupAttribute(string groupName) { ErrorMessage = string.Format(“You must select at least one value from group \”{0}\””, groupName); GroupName = groupName; } public string GroupName { get; … Read more
You may try this (Example): $(function(){ $(“#myform”).validate(); $(“#myform”).on(‘submit’, function(e) { var isvalid = $(“#myform”).valid(); if (isvalid) { e.preventDefault(); alert(getvalues(“myform”)); } }); }); function getvalues(f) { var form=$(“#”+f); var str=””; $(“input:not(‘input:submit’)”, form).each(function(i){ str+=’\n’+$(this).prop(‘name’)+’: ‘+$(this).val(); }); return str; }