MVC 5 - Validate a specific field on client-side - javascript

I want to populate a city/state drop down list based on the postal code a user types into a textbox. So when the text changes, I'm going to make an ajax call to retrieve the data. However, I only want to perform that ajax request for valid postal codes. The field already validates using the DataAnnotations.RegularExpression attribute and jquery.validate.unobtrusive validation library. I'm unclear on what can and can't be used from jquery.validate when using unobtrusive. I've looked at the unobtrusive code, but haven't gotten an understanding of it yet. So two questions:
Using javascript,
is there a way to force validation on a specific field, not the whole form?
is there a way to check whether a specific field is valid?

After digging around in the source code, I've come to these conclusions. First, the purpose of unobtrusive is to wire up the rules and messages, defined as data- attributes on the form elements by MVC, to jQuery.validation. It's for configuring/wiring up validation, not a complete wrapper around it, so when it comes to performing validation that is already set up, you don't have to worry about "circumventing", or not involving, unobtrusive.
So to answer the questions:
Yes, there are two ways. The Validator.element(element) function and the $.fn.valid() extension method. .valid actually calls Validator.element internally. The difference is .valid works on a jQuery which allows you to perform the validation on one or more fields (or the form itself). Validator.element performs validation on only a single element and requires you to have an instance of the validator object. Although the documentation states .validate() "validates the selected form", it actually appears to initialize validation for the form, and if it has already been called, it simply returns the validator for the form. So here are examples of two ways to validate an input (below #2).
Yes, but not without also performing the validation. Both of the methods from #1 return a boolean so you can use them to determine whether a field is valid. Unfortunately there doesn't appear to be anything exposed by the library that allows you to check the validation without, in effect, showing or hiding the validation message. You would have to get at and run the rule(s) for the field from your code, which may be possible, but my need didn't justify spending the time on it.
Example:
<form>
<input id="txtDemo" type="text"></input>
</form>
...
<script type="text/javascript">
$("#txtDemo").valid();
//or
//Get the form however it makes sense (probably not like this)
var validator = $("form").validate();
//Note: while .element can accept a selector,
//it will only work on the first item matching the selector.
validator.element("#txtDemo");
</script>

you can find if a single field is valid and trigger this validation this way:
$("#myform").validate().element("#elem1");
details here http://docs.jquery.com/Plugins/Validation/Validator/element#element

Use like this:
$('#Create').on('click', function () {
var form = $('#test').closest('form');
$(form).validate();
if (!$(form).valid()) {
return
} else {
// Bide the data
}
});
Hope it works for you

Related

jQuery Validator: Validate form that is not attached to DOM

I'm having trouble when I try to validate a form with the jQuery Validator plugin. So in this case, I have constructed a div table (not table elements, but divs styled as a table) where each row essentially is its own form complete w/ whatever form-controls.
Since this table could have potentially hundreds of rows however, I didn't want the overhead of wrapping each row element in its own separate form. So I came up with an idea where the save method just takes the row itself as an arg in order to create/validate/submit a form behind the scenes.
What I do is essentially clone the row and append the clone onto a new form I create like so
$("<form></form>").append(clonedRow);
Then I take the resulting variable (the form) and setup the validation. The validation options/setup look like this:
form.validate({
rules: {
Follow_Up_Code: { required: true, maxlength: 2 }
},
messages: {
Follow_Up_Code: { required: "...", maxlength: "..." }
},
submitHandler: function(){...}
});
Currently I only want to validate against one input in this row to test. Here is the input markup:
<input type="text" class="get set input" name="Follow_Up_Code" style="width: 100%;">
After the form runs .validate(), I immediately submit the form. However, for my test case the form passes validation and runs the SubmitHandler set up in the validator every time.
This leads me to think that any form I want to validate with this plugin needs to be attached to the DOM in order to validate properly. However I've found no documentation on this. I would like to know if there is a way I could fix this, or if I'm better off trying to find/create my own validation service for this purpose.
After testing, I've come to discover that. Yes, the form needs to be attached to the DOM in some way shape or form. However, you can get around this pretty easily and unobtrusively:
form.css({ position: "absolute", visibility: "hidden", left: "-5000px" });
$("body").append(frm);
frm.submit();
That way the user won't be able to see or interact with this form while it's on the DOM.
This will get the job done and validate the form. In this case however, if there were any validation errors that I would want to show on the original table. I would have to access the invalid controls from inside the validator's invalidHandler option on setup. From there I just need to insert the error message(s) at where my desired location happens to be.
After all is said and done, it would be a good idea to .destroy the validator and remove that form from the DOM. I believe doing this at the end of both the submit handler and invalid handler would be the best way to go about that.

Make jQuery validator validate hidden inputs on submit [duplicate]

In the new version of jQuery validation plugin 1.9 by default validation of hidden fields ignored. I'm using CKEditor for textarea input field and it hides the field and replace it with iframe. The field is there, but validation disabled for hidden fields. With validation plugin version 1.8.1 everything works as expected.
So my question is how to enable validation for hidden fields with v1.9 validation plugin.
This setting doesn't work:
$.validator.setDefaults({ ignore: '' });
The plugin's author says you should use "square brackets without the quotes", []
http://bassistance.de/2011/10/07/release-validation-plugin-1-9-0/
Release: Validation Plugin 1.9.0:
"...Another change should make the setup of forms with hidden elements
easier, these are now ignored by default (option “ignore” has
“:hidden” now as default). In theory, this could break an existing
setup. In the unlikely case that it actually does, you can fix it by
setting the ignore-option to “[]” (square brackets without the
quotes)."
To change this setting for all forms:
$.validator.setDefaults({
ignore: [],
// any other default options and/or rules
});
(It is not required that .setDefaults() be within the document.ready function)
OR for one specific form:
$(document).ready(function() {
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
});
EDIT:
See this answer for how to enable validation on some hidden fields but still ignore others.
EDIT 2:
Before leaving comments that "this does not work", keep in mind that the OP is simply asking about the jQuery Validate plugin and his question has nothing to do with how ASP.NET, MVC, or any other Microsoft framework can alter this plugin's normal expected behavior. If you're using a Microsoft framework, the default functioning of the jQuery Validate plugin is over-written by Microsoft's unobtrusive-validation plugin.
If you're struggling with the unobtrusive-validation plugin, then please refer to this answer instead: https://stackoverflow.com/a/11053251/594235
This worked for me, within an ASP.NET MVC3 site where I'd left the framework to setup unobtrusive validation etc., in case it's useful to anyone:
$("form").data("validator").settings.ignore = "";
Make sure to put
$.validator.setDefaults({ ignore: '' });
NOT inside $(document).ready
So I'm going to go a bit deeper in to why this doesn't work because I'm the kind of person that can't sleep at night without knowing haha. I'm using jQuery validate 1.10 and Microsoft jQuery Unobtrusive Validation 2.0.20710.0 which was published on 1/29/2013.
I started by searching for the setDefaults method in jQuery Validate and found it on line 261 of the unminified file. All this function really does is merge your json settings in to the existing $.validator.defaults which are initialized with the ignore property being set to ":hidden" along with the other defaults defined in jQuery Validate. So at this point we've overridden ignore. Now let's see where this defaults property is being referenced at.
When I traced through the code to see where $.validator.defaults is being referenced. I noticed that is was only being used by the constructor for a form validator, line 170 in jQuery validate unminified file.
// constructor for validator
$.validator = function( options, form ) {
this.settings = $.extend( true, {}, $.validator.defaults, options );
this.currentForm = form;
this.init();
};
At this point a validator will merge any default settings that were set and attach it to the form validator. When you look at the code that is doing the validating, highlighting, unhighlighting, etc they all use the validator.settings object to pull the ignore property. So we need to make sure if we are to set the ignore with the setDefaults method then it has to occur before the $("form").validate() is called.
If you're using Asp.net MVC and the unobtrusive plugin, then you'll realize after looking at the javascript that validate is called in document.ready. I've also called my setDefaults in the document.ready block which is going to execute after the scripts, jquery validate and unobtrusive because I've defined those scripts in the html before the one that has the call in it. So my call obviously had no impact on the default functionality of skipping hidden elements during validation. There is a couple of options here.
Option 1 - You could as Juan Mellado pointed out have the call outside of the document.ready which would execute as soon as the script has been loaded. I'm not sure about the timing of this since browsers are now capable of doing parallel script loading. If I'm just being over cautious then please correct me. Also, there's probably ways around this but for my needs I did not go down this path.
Option 2a - The safe bet in my eyes is to just replace the $.validator.setDefaults({ ignore: '' }); inside of the document.ready event with $("form").data("validator").settings.ignore = "";. This will modify the ignore property that is actually used by jQuery validate when doing each validation on your elements for the given form.
Options 2b - After looking in to the code a bit more you could also use $("form").validate().settings.ignore = ""; as a way of setting the ignore property. The reason is that when looking at the validate function it checks to see if a validator object has already been stored for the form element via the $.data() function. If it finds a validator object stored with the form element then it just returns the validator object instead of creating another one.
This worked for me within an ASP.NET site.
To enable validation on some hidden fields use this code
$("form").data("validator").settings.ignore = ":hidden:not(#myitem)";
To enable validation for all elements of form use this one
$("form").data("validator").settings.ignore = "";
Note that use them within $(document).ready(function() { })
Just added ignore: [] in the specific page for the specific form, this solution worked for me.
$("#form_name").validate({
ignore: [],
onkeyup: false,
rules: {
},
highlight:false,
});
This is working for me.
jQuery("#form_name").validate().settings.ignore = "";
The validation was working for me on form submission, but it wasn't doing the reactive event driven validation on input to the chosen select lists.
To fix this I added the following to manually trigger the jquery validation event that gets added by the library:
$(".chosen-select").each(function() {
$(this).chosen().on("change", function() {
$(this).parents(".form-group").find("select.form-control").trigger("focusout.validate");
});
});
jquery.validate will now add the .valid class to the underlying select list.
Caveat: This does require a consistent html pattern for your form inputs. In my case, each input filed is wrapped in a div.form-group, and each input has .form-control.
Just find the text ignore: ":hidden" in your jquery validation file and comment it.
After comment this it will never loss any hidden elements to validate...
Thanks

JQuery change behaving inconsistently across browsers

I'm new to JavaScript and I'm having a bit of a problem with some JQuery.validate code that I've inherited. Please forgive me if the answer is obvious :)
I have the following validation method declaration:
jQuery.validator.addMethod("myMethod", function(value, element, param) {
/*...*/
if (document.getElementById("DOMID") {
/*...*/
}
/*...*/
)};
Essentially the method is checking for the existence of a certain DOM object and it sets some different validation parameters if it does exist. The object I want to validate is a set of <select> inputs wrapped in a parent <div id="DOMID">, but there are a couple of different "DOMID" cases which each have slightly different validation rules - hence the check inside the method. I want to validate the <select>s each time one of them changes their value. I then have this activation:
$(".myClass").validate({
onchange: true,
});
With myClass being applied to all of the <select> inputs I want to validate.
Lastly, I have this registration:
jQuery.validator.addClassRules(
{
myClass:
{
myMethod: true,
}
}
This works just fine in Chrome - validation is applied each time I change one of the <select> options.
However, in Firefox, this is not the case - I find that I have to blur the select in order for the validation to activate. So, that's one question: Why does change behave differently between Chrome and Firefox? Or is that not the case, and there's something else in my code which is responsible for this inconsistency?
The second question I have is about something I find quite peculiar. In attempting to fix the issue of Firefox not validating until blur, I changed my JavaScript getElementByID... to use jQuery, instead:
if ($("#DOMID").length > 0) { [...]
(I also tried if ($("#DOMID").get(0)) but they do the same thing, I think - unfortunately, neither made Firefox behave properly)
The weird part is that after I made this change, Chrome stopped validating on change and would only validate on blur. That's the only line of code I made a change to - I've since switched it back and Chrome behaves properly again. Why does that happen? How would the internals of the validation method affect when it was being called?
Hopefully this question makes sense - please let me know if any additional information would be helpful! Also any words of wisdom for debugging this problem would be very appreciated. Thanks very much for any advice!
You're using the jQuery Validate plugin improperly...
Quote OP:
"I want to validate the <select>s each time one of them changes their value. I then have this activation:"
$(".myClass").validate({
onchange: true,
});
1) You cannot attach .validate() to any single input element. The .validate() method can only be attached to the <form> element...
$('#myForm').validate({
// options, rules, etc.
});
2) There is no such plugin option called onchange. You are only allowed to use the options that were created by the developer specifically for this plugin.
If you want to add and remove rules based on a select element, then use the .rules('add') and .rules('remove') methods.
$('#myselect').on('change', function() {
$('#myInput').rules('add', { // add rule(s) to #myInput
required: true
});
});
If instead you want to "validate" or test the validity of something, use the .valid() method.
$('#myselect').on('change', function() {
$('#myInput').valid(); // trigger a test of #myInput
});

Disable required validation by JavaScript

I have a create form to create an object.
The create model has some properties that are only visible (.hide, .show()) if a checkbox is checked and that are marked required (by Attribute in Model).
Unfortunatly when the checkbox is not checked, the required validation is performed on the properties hidden.
How can I disable the required validation for this properties?
I tried setting the data-val property of the input element to false but this does not work.
Some an idea?
Thanks in advance
Tobias
UPDATE:
Here is the java script code. The data-val property is set correctly to false. it seems that validation does not care of this property. there is also the data-val-required attribute but there is a text i could not backup.
$(function () {
$("#MyCheckbox")
.change(function () {
if (this.checked) {
$("#divWithChildProperties [data-val]").attr("data-val", true);
$("#divWithChildProperties ").show();
}
else {
$("#divWithChildProperties [data-val]").attr("data-val", false);
$("#divWithChildProperties ").hide();
}
})
});
I've handled cases like this with a custom Validation Attribute. Instead of using the Required attribute for properties you could make a RequiredIf attribute and make some of your properties required only if another property is a certain value.
Here is a post about creating an attribute like this (the example in the middle of the page under the 'A more complex custom validator' section): http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
If your checkbox represents a property in your model this should work fine.
If you don't want to handle this with a new validation attribute you will have to do a few more things than just change the data-val attribute to false. When jQuery validate first parses your form it stores values in the form's data. So simply changing data-val isn't enough. You will additionally have to remove the stored validation data and reparse the form. Here's an example:
// Do this after you've removed/modified the data-val attribute
$('selector for your form').removeData('unobtrusiveValidation');
$('selector for your form').removeData('validator');
$.validator.unobtrusive.parse('selector for your form');
You can use following JQuery to remove all validation rules of your element
$('#ElementId').rules('remove');
Same way you can use class name like,
$('.ElementClassName').rules('remove');
If you want to remove specific rule, do like this
$('#ElementId').rules('remove', 'required');
The unobtrusive javascript plugin provided by MVC doesn't process the data properties on the fly. Instead, it parses the results on document ready and caches them.
Try calling $.validator.unobtrusive.parse(myForm) on your form after modifying the property in question to see if it gives you expected results.
Unobstrusive validation looks for this attribute data-val="true"
I guess, that if you do a $('#mycheckbox').data('val','false'), the validation will skip a item with that id.
Probably there is a more appropriate way to do it, but if not, take this.
cheers.
There are many ways to disable unobtrusive validation in Javascript but most of them seems a bit hackish...
Recently found that you can do it with submit button. Check this link for info
http://www.nitrix-reloaded.com/2013/09/16/disable-client-side-validation-on-a-button-click-asp-net-mvc/
//this
<script type="text/javascript">
document.getElementById("backButton").disableValidation = true;
</script>
//or this
<input type="submit" name="backButton" value="Back"
title="Go back to Prev Step" disableValidation="true" />
//or this
<input type="submit" name="backButton" value="Back"
title="Go back to Prev Step" class="mybtn-style cancel" />
Another way that is more flexible but more complicated : you can disable unobtrusive validation by setting the data-val attribute to false. But there is a trick...
Unobtrusive validation data is cached when the document is ready. This means that if you have data-val='true' at the beginning and that you change it later on, it will still be true.
So, if you want to change it after the document is ready, you also need to reset the validator which will erase the cached data. Here is how to do it :
disableValidation: function () {
//Set the attribute to false
$("[data-val='true']").attr('data-val', 'false');
//Reset validation message
$('.field-validation-error')
.removeClass('field-validation-error')
.addClass('field-validation-valid');
//Reset input, select and textarea style
$('.input-validation-error')
.removeClass('input-validation-error')
.addClass('valid');
//Reset validation summary
$(".validation-summary-errors")
.removeClass("validation-summary-errors")
.addClass("validation-summary-valid");
//To reenable lazy validation (no validation until you submit the form)
$('form').removeData('unobtrusiveValidation');
$('form').removeData('validator');
$.validator.unobtrusive.parse($('form'));
},
You don't need to reset all the messages, input styles and validation summary to be able to submit the form but it's useful if you want to change the validation after the page is loaded. Otherwise, even if you change the validation, the previous error messages will still be visible...
The default DataAnnotationsModelValidatorProvider adds a Required attribute to all value types. You can change this behavior by adding the code in this answer.
You could implement a custom validator like "RequiredIf".
That will keep your model design quite obvious (unlike client-side-only solutions proposed). This allows you to keep the validation logic separate from display logic (that's what MVC is all about).
See this answer : RequiredIf Conditional Validation Attribute
and ultimately that blog post : Conditional Validation in ASP.NET MVC 3
cheers!

Make jquery validation check for new form elements, or manually add them

I'm using jQuery validation plugin to validate a form. when one thing is selected in a selectbox a new selectbox is added trough ajax. this new element has class required but when I try to submit, it is not validated.
I know this is because when the validation plugin is initialized on the form the classes are checked to see which validations will be necessary. When a new item is added it is not checked for any classes, or initialized at all for that matter.
Is there a way to add the selectbox to the validation after it is initialized, maybe reinit the validation making it use its previous settings but rechecking its elements?
You have a couple options here. In jquery.validate.js there is a rules plugin. You can use it like so:
$field.rules('add', {required: true }); // additional rules
The plugin uses the fields' form to figure out what validator should be associated with the field. Documentation here
If you want the validation rules to come from the data annotations it is a little more complicated. Let me know what you are going for.
Found the solution with a fresh look, i found out that my selectboxes all had the name of undefined because of a mis-capitalized varname. This means only the first selectbox got validated. $field.rules('add', {required: true }); was not necessary because the class required did take this time.

Categories

Resources