tags:

views:

1184

answers:

2

How to validate special characters in a textbox without using Validate plugin on JQuery

+1  A: 

Try this javascript

function checkSpecialKeys(e) {
            if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 40)
                return false;
            else
                return true;
        }

Implement it on the onkeydown or onkeyup attribute of the textbox.

You can add or remove the keys you which to validate by specifying the key code.

Raúl Roa
+4  A: 

If you want to delete special characters and allow only words, spaces and numbers:

$(document).ready(function(){
  $("input").keyup(function(){
     var text=$(this).val();
     $(this).val(text.replace(/[^\w\d\s]/,""));
  })
})
mck89
+1 I'm a year late but very helpful answer, thanks
kekekela