I want to display error for values that are duplicates or not unique, but my form takes in array of inputs, I have checked these questions on jsfiddle, name = "week[]" fails but name = "week" works fine
Form HTML
<html>
<head></head>
<body>
<form name = "myForm" id = "myForm" class ="validate">
<input type="number" name="week[]" id="week1"/>
<input type="number" name="week[]" id="week2"/>
<input type="number" name="week[]" id="week3"/>
<input type="number" name="week[]" id="week4"/>
</form>
<script src="assets/js/jquery.validate.min.js"></script>
</body>
</html>
I tried this
<script type="text/javascript">
jQuery.validator.addMethod("unique", function(value, element, params) {
var prefix = params;
var selector = jQuery.validator.format("[name!='{0}'][unique='{1}']", element.name, prefix);
var matches = new Array();
$(selector).each(function(index, item) {
if (value == $(item).val()) {
matches.push(item);
}
});
return matches.length == 0;
}, "Value is not unique.");
jQuery.validator.classRuleSettings.unique = {
unique: true
};
</script>
Any help is appreciated
IDs.idshould be unique. And please show us your code that validates the input fields... or at least what you have tried so far..validate()and the HTML markup of the form? See: stackoverflow.com/help/mcve