user335160
user335160

Reputation: 1362

prevent special characters in input fields using jquery

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

Upvotes: 2

Views: 4132

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176956

Simple function to allow 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;
}

Upvotes: 1

Jla
Jla

Reputation: 11384

You can also check the more specific alphanumeric plugin

Example:

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

Allows alphabet and numeric characters

Upvotes: 1

Related Questions