1

This is my input:

<input type='text' class='myprofiletags' name='my_profile_tags' value='' />

I want to alert and forbid the user if he/she writes the same word twice. I am entering values with commma e.g

games, animes, movies

jQuery('.myprofiletags').keypress(function(e)
    {
       if (jQuery(this).val().match(/,/g).length >= 10)
       {
            if(e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)
            {

            }
            else
            {
                e.preventDefault();  // stops the keypress occuring
                alert('No more profile tags are allowed');                                      
            }
         }
    });
Hassan Ali
  • 593
  • 1
  • 8
  • 26

1 Answers1

0
  • Set an event handler (onkeyup) on input box so that whenever user hits a key the handler gets called
  • In event handler take the values of input box and split them on comma
  • Check if any value is duplicate
  • If no return true
  • If yes show an alert

Synopsis:

$(".myprofiletags").keydown (function (e) {
    alert ($(this).val());
});

You can also use change

$('input[name^="text"]').change(function() {

Edit: You are going in right direction, just split the value on comma and use each splitted value as an item of array.

See how to do that:

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133