使用Jquery验证防止重复值 [英] prevent Duplicate values using Jquery Validation

查看:111
本文介绍了使用Jquery验证防止重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have form and form text field which generates dynamically using JSP. And I'm using Jquery validation but want to add functionlaty to prevent duplicate entry in the form.

例如

<form name="myForm" id="myForm">
      <input type="text" name="text1" id="text1">
      <input type="text" name="text2" id="text2">
      <input type="text" name="text3" id="text3">
      =
      = 
      N number of form fields
      <input type="text" name="textn" id="textn">

</form>

我想检查是否使用jQuery验证在文本字段中输入了重复值.

I want to check if there is any duplicate value entered in the textfield using jQuery validation.

推荐答案

类似的方法应该起作用:

Something like this should work:

$(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就会循环遍历表单中的所有文本框,并将它们的值与用户刚刚输入的值进行比较(如果存在重复值).找到,然后提醒用户.

In a nutshell, how this works is: Whenever a user enters something into a text box, JQuery loops through all the text boxes in the form and compares their values with the value that the user just entered, if a duplicate value is found, then alert the user.

这篇关于使用Jquery验证防止重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆