Check input with jQuery.contains ()

I want to validate a form using jQuery. Therefore, I need to check if the input field does not contain ("#validatename")several words, for example, “United States of America”, “United Kingdom” or “France”.

To ban the "United States of America" ​​(I will see later an array with all the forbidden lines!), I have ...

$("#validatename").keyup(function()
{
 var name = $("#validatename").val();
 if(name != '')
 {
   if ($('#validatename:contains("United States of America")').length > 0) // http://stackoverflow.com/questions/902597/jquery-checking-to-see-if-div-contains-text-then-action
   {
    $("#validatename").css({
    "background-image": "url('no.png')"
    });
   }
   else
   {
    $("#validlastname").css({
    "background-image": "url('yes.png')"
    });
   }
 }
}

... But this does not work: yes.pngalways displayed!

Any help or idea would be greatly appreciated!

Many thanks.

+5
source share
2 answers

try

$("#validatename").val().indexOf("United States of America") > -1
+8
source

instead

$('#validatename:contains("United States of America")').length > 0

you can use

name.indexOf("United States of America") != -1
+2
source

All Articles