views:

79

answers:

2

Hello everyone.

So i have this page wich 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!

Thanks in advance ;)

+1  A: 

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

Kelsey
thank you for your answer!
rolando
+1  A: 

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;
T B
this was exactly what i needed, something simple and easy to implement, thank you very much
rolando