jQuery keyup() illegal characters

前端 未结 3 1084
时光说笑
时光说笑 2021-01-25 17:08

I have a field and want to prevent some illegal characters while showing the user as he types. How can I do this in follow example?

  $(\'input\').bind(\"change          


        
3条回答
  •  情歌与酒
    2021-01-25 17:26

    You may use the indexOf to determine if a string contains a certain char...

    vowels = array('"', "<", ">", "&");

    $('input').bind("change keyup", function()
    {
       var val = $(this).attr("value");
       var illegal = '';       
       $(vowels).each(function()
       {
          if(val.indexOf(this)!=-1)
          {
              illegal= this;
              break;
          }
       });
       if(illegal != '') 
       {
           $(this).css("background", "red");
           val = val.replace(illegal, "");
           $(this).attr("value", val)
       }
    });
    

提交回复
热议问题