使用Jqueryvalidation防止重复值

我有使用JSP动态生成的表单和表单文本字段。 我正在使用Jqueryvalidation,但想添加functionlaty以防止表单中的重复输入。

例如

= = N number of form fields

我想检查是否使用jQueryvalidation在文本字段中输入了任何重复值。

像这样的东西应该工作:

 $(function(){ $('input[name^="text"]').change(function() { var $current = $(this); $('input[name^="text"]').each(function() { if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id')) { alert('duplicate found!'); } }); }); }); 

简而言之,这是如何工作的:每当用户在文本框中输入内容时,JQuery循环遍历表单中的所有文本框,并将其值与用户刚刚输入的值进行比较,如果找到重复值,则提醒用户。

我对jqueryvalidation很新,但我有一个类似的问题要解决,我提出了以下解决方案(使用以前的答案获取灵感!):

使用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 }; 

用法:

   

演示: http : //jsfiddle.net/mysteryh/bgzBY/

Bug Magnet你的答案不是扩展jqueryvalidation的function。 它是一个单独的脚本。

您可以通过在In jquery.validate.js中进行这些更改来实现此目的:

第275行:为唯一方法添加消息

 messages: { unique: "This answer can not be repeated.", 

第744行:为unique添加新的类规则

 classRuleSettings: { unique: { unique: true }, 

最后一行899:添加新的唯一方法

 methods: { unique: function (value, element) { var parentForm = $(element).closest('form'); var timeRepeated = 0; $(parentForm.find('input:type=text')).each(function () { if ($(this).val() === value) { timeRepeated++; } }); if (timeRepeated === 1 || timeRepeated === 0) { return true } else { return false } }, 

现在,只需在调用validation函数时,您就可以使用这样的独特方法:

 $("#signupForm").validate({ rules: { firstname: "required", lastname: "required", username: { required: true, unique: true, minlength: 2 }, 

对于这篇文章的新观众而言,它是值得的…我似乎无法让这些解决方案适合我(我正在使用v1.13.1)…所以我做了一点Frankensteining和拼凑的比特从几个答案的碎片一起创造我需要的东西(并反过来为这个问题创建另一个答案)。 我的场景类似于@Vicky,因为我需要字段都是唯一的。 但是,如果该字段不是必填字段,那么它可以为空。

使用@XGreen的部分解决方案,以及@Stephen Bugs Kamenar的建议,这是我提出的解决方案:

 $.validator.addMethod("unique", function(value, element) { var parentForm = $(element).closest('form'); var timeRepeated = 0; if (value != '') { $(parentForm.find(':text')).each(function () { if ($(this).val() === value) { timeRepeated++; } }); } return timeRepeated === 1 || timeRepeated === 0; }, "* Duplicate"); 

如何使用

要使用此解决方案,您需要做的就是将值为unique添加到您想要唯一的输入字段中:

   

在上面的示例中,如果firstName和lastName的值相同,jQuery Validate将抛出错误。

适用于jQuery.Validate v1.13.1或更高版本。

如果我没有弄错的话,你可以稍微缩小重复的比较。 例如,我只想查看同一个类的其他字段,比如’name’:

 $(parentForm.find('.name')).each(function () { if ($(this).val() === value) { timeRepeated++; } 

或者可能:

 $('.name').each(function () { if ($(this).val() === value) { timeRepeated++; } 
   function checkName(thisvalues) { var currentval = $('#' + thisvalues.id).val(); $('.name').each(function () { console.log(this.value + ',' + this.id); if (currentval == this.value && thisvalues.id != this.id) { alert('With' + this.value+ 'name already exists'); } }); } 

我不会使用Validator插件,但是,也许这个http://jsfiddle.net/6dbrjbg6/2/可以帮助:

 function ISeeWhatYouDidThere(e) { var $repeat = 0, $val = e.val(), $parent = "#" + e.closest("div").attr("id") ; if($.trim($val) == "") return; e.removeClass("ui-state-error"); $("input", $parent).each(function() { if($(this).val() == $val) $repeat++; }); if($repeat <= 1) return; alert("Repetido!"); e.addClass("ui-state-error"); } ///////////////////////////////// $("input","div").blur(function(){ ISeeWhatYouDidThere( $(this) ); }); 

这应该工作:

HTML

 

JQuery的

 $(function(){ $('button').click(function(){ var len = $('input').length,i,k; $('input').css({'border':'solid 1px #888'}) for(i=0; i < len; i++){ var v = $('input:eq('+ i +')').val(); for( k=0; k < i; k++ ){ var s = $('input:eq('+ k +')').val(); if( s == v ){ $('input:eq('+ i +')').css({'border':'solid 1px #F00'}) $('input:eq('+ k +')').css({'border':'solid 1px #F00'}) return false; } } } }) }) 

我知道这是一个老问题,答案与原来的问题略有偏离,但是我把头发拉了一段时间因为我只想检查一个特定的字段VS另一个特定的字段而无法让它工作任何另一种方式。 jQueryvalidation带有一个equalTovalidation器。 https://jqueryvalidation.org/equalTo-method/

在源代码中看起来像:

 equalTo: function( value, element, param ) { // Bind to the blur event of the target in order to revalidate whenever the target field is updated var target = $( param ); if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) { target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() { $( element ).valid(); } ); } return value === target.val(); } 

获取此代码并使用addMethod函数,如此交换===!==

 jQuery.validator.addMethod("notEqualTo", function(value, element, param) { // Bind to the blur event of the target in order to revalidate whenever the target field is updated var target = $( param ); if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) { target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() { $( element ).valid(); } ); } return value !== target.val(); // condition returns true or false }, "Values must be unique"); 

然后在规则部分申请:

  'first_name':{ required: true, notEqualTo: '#last_name' }