Skip to main content

prevent Duplicate values using Jquery Validation


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.



E.g.




<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>



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



Thanks


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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!');
    }

    });
    });
    });


    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.

    ReplyDelete
  2. Bug Magnet your answer is not extending the functionality of jquery validate. its a separate script.

    You can achieve this by making these changes in In jquery.validate.js:

    line 275: add a message for the unique method

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


    line 744: add a new class rule for unique

    classRuleSettings: {
    unique: { unique: true },


    and finally line 899: add the new unique method

    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
    }
    },


    now simply when you call your validate function you can use a unique method like this:

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

    ReplyDelete
  3. If I'm not mistaken, you could narrow the duplicate comparison down a bit. For example, I just want to look at other fields of the same class, say 'name':

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


    or maybe:

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

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex