Click here to Skip to main content
15,904,024 members

Comments by huotari (Top 18 by date)

huotari 4-Oct-12 5:17am View    
and don't place these $.validator.addMethod and $.validator.unobtrusive.adapters.addBool inside document ready
huotari 4-Oct-12 5:15am View    
oops replace if(numOfEmptyInputs > 0) with if(numOfEmptyInputs > 1)
huotari 4-Oct-12 5:14am View    
But like i said this code just validates the last input or select. Try the following:

$.validator.addMethod('multiplerequiredvalues', function (value, element) {
if ($(element).is(':visible')) {
var name = $(element).attr('name');
var elements;
if ($(element).is('input')) {
elements = $('input[name=' + name + ']');
}
else {
elements = $('select[name=' + name + ']');
}
var numOfEmptyInputs = 0;
elements.each(function () {
if ($(this).is(':visible') && !$(this).val()) {
numOfEmptyInputs++;
}
});
if(numOfEmptyInputs > 0)
return true;
else
return false;
}
else {
return true;
}
});
$.validator.unobtrusive.adapters.addBool("multiplerequiredvalues");
huotari 3-Oct-12 10:26am View    
I think this piece of code just validates the last input or select, if it has a value then validation passes. Also from all the examples i have seen, this should not be inside document ready.

(function ($) {
$.validator.addMethod('multiplerequiredvalues', function (value, element) {
if ($(element).is(':visible')) {
var returnVal = true;
var name = $(element).attr('name');
var elements;
if ($(element).is('input')) {
elements = $('input[name=' + name + ']');
}
else {
elements = $('select[name=' + name + ']');
}
elements.each(function () {
if ($(this).is(':visible')) {
returnVal = $(this).val() != "" && $(this).val() != null;
}
});
return returnVal;
}
else {
return true;
}
});
$.validator.unobtrusive.adapters.addBool("multiplerequiredvalues");
} (jQuery));
huotari 3-Oct-12 8:44am View    
So during the first load you cant see this pages scripts at all?