Skip to content

Instantly share code, notes, and snippets.

@shinespark
Created November 7, 2015 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinespark/45a6d2592387724f1078 to your computer and use it in GitHub Desktop.
Save shinespark/45a6d2592387724f1078 to your computer and use it in GitHub Desktop.
check duplicate for jquery validation plugin
/*
* 指定: {notDuplicate: '.class-name'}
* なお、指定された要素が空, 未選択に戻された際はチェックokとする
*/
jQuery.validator.addMethod('notDuplicate', function( value, element, param ) {
if ($(element).val() === '') {
return true;
}
var that = this;
var bool = true;
$(param).each(function() {
if (element === $(this).get(0)) { // 自身との同一チェックは行わない
return true;
}
bool = bool && !$.validator.methods.equalTo.call(that, value, element, this);
});
return this.optional(element) || bool;
}, '異なる項目をご選択ください' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment