使用jquery防止输入字段中的特殊字符

时间:2022-08-18 11:51:34

I want to block all special characters in the input fields using jquery, any suggestion in doing this?

我想使用jquery阻止输入字段中的所有特殊字符,这样做有什么建议吗?

2 个解决方案

#1


1  

You can also check the more specific alphanumeric plugin

您还可以检查更具体的字母数字插件

Example:

$('.sample1').alphanumeric();

Allows alphabet and numeric characters

允许使用字母和数字字符

#2


1  

Simple function to allow alpha only char :

简单的功能,允许alpha only char:

function AlphaNumericOnly(e,isAlphaonly)
{
   // copyright 1999 Idocs, Inc. http://www.idocs.com
   var key = [e.keyCode||e.which];

   var keychar = String.fromCharCode([e.keyCode||e.which]);
   keychar = keychar.toLowerCase();

   if(isAlphaonly=='true')
         checkString="abcdefghijklmnopqrstuvwxyz";
   else 
         checkString="abcdefghijklmnopqrstuvwxyz0123456789";

   if ((key==null) || (key==0) || (key==8) || 
         (key==9) || (key==13) || (key==27) )
        return true;
   else if (((checkString).indexOf(keychar) > -1))
        return true;
   else
        return false;
}

#1


1  

You can also check the more specific alphanumeric plugin

您还可以检查更具体的字母数字插件

Example:

$('.sample1').alphanumeric();

Allows alphabet and numeric characters

允许使用字母和数字字符

#2


1  

Simple function to allow alpha only char :

简单的功能,允许alpha only char:

function AlphaNumericOnly(e,isAlphaonly)
{
   // copyright 1999 Idocs, Inc. http://www.idocs.com
   var key = [e.keyCode||e.which];

   var keychar = String.fromCharCode([e.keyCode||e.which]);
   keychar = keychar.toLowerCase();

   if(isAlphaonly=='true')
         checkString="abcdefghijklmnopqrstuvwxyz";
   else 
         checkString="abcdefghijklmnopqrstuvwxyz0123456789";

   if ((key==null) || (key==0) || (key==8) || 
         (key==9) || (key==13) || (key==27) )
        return true;
   else if (((checkString).indexOf(keychar) > -1))
        return true;
   else
        return false;
}