jQuery Validation Plugin – adding rules that apply to multiple fields

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

Phone Number Validation MVC

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)

Apply Twitter Bootstrap Validation Style and Message to ASP.NET MVC validation

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

Why is being given NoValidate attribute?

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.

MVC3 Validation – Require One From Group

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

jquery validation: prevent form submit

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; }