jQuery validate (at least one field has text)
I have this page which has several TextAreas (asp.net mvc).
I need to开发者_如何学运维 validate that at least one of those fields has text in it. How can I do that with jQuery?<%= Html.TextArea("taMetodologia",Model.Enmienda.Detalles.EnmiendaMetodologia, 8, 70,new {@class = "Especificaciones"}) %>
That's an example of the textAreas I have.
Assuming you are not using the validate plugin and all the textareas have the "Especificaciones" class, you could use a filter on the class.
If the length of the following is greater than 0, then at least one of the textareas had a value in it.
$(".Especificaciones").filter(function(){
return $(this).val() != "";
}).length;
Check out this link as it should be what your looking for:
jQuery validation: Indicate that at least one element in a group is required
Quoted from the above link:
To start with, I put class="required_group" on each of the elements in the group. Then, I added a custom validation method:
jQuery.validator.addMethod('required_group', function(val, el) {
var $module = $(el).parents('div.panel');
return $module.find('.required_group:filled').length;
});
... a custom class rule to take advantage of the new method:
jQuery.validator.addClassRules('required_group', {
'required_group' : true
});
... and finally a custom message for the new method:
jQuery.validator.messages.required_group = 'Please fill out at least one of these fields.';
Here is another link to someone that used this method but tweaked it:
jquery validation only one field in a group required
精彩评论