开发者

Check an input with jQuery.contains()

开发者 https://www.devze.com 2023-02-07 07:26 出处:网络
I want to validate a form with jQuery. So I need to check if an input field (\"#validatename\") does not contain few words, like \'United States of America\', \'United Kingdom\' or \'France\' for inst

I want to validate a form with jQuery. So I need to check if an input field ("#validatename") does not contain few words, like 'United States of America', 'United Kingdom' or 'France' for instance.

To forbidden 'United State开发者_开发百科s of America" (I'll see later for an array with all forbidden strings!), 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 it doesn't work: yes.png is always displaying!

Any help or idea will be greatly appreciated!

Thanks a lot.


try

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


instead of

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

you can use

name.indexOf("United States of America") != -1
0

精彩评论

暂无评论...
验证码 换一张
取 消