Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Created February 24, 2016 06:47
Show Gist options
  • Save johndavedecano/a945d6d94d472c922556 to your computer and use it in GitHub Desktop.
Save johndavedecano/a945d6d94d472c922556 to your computer and use it in GitHub Desktop.
Block Special Characters while Typing
function RestrictSpaceSpecial(e) {
var exp = String.fromCharCode(window.event.keyCode)
//Below line will have the special characters that is not allowed you can add if you want more for some characters you need to add escape sequence
var r = new RegExp(/^[a-zA-Z0-9-_]+$/);
if (exp.match(r)) {
return true;
} else {
window.event.keyCode = 0
return false;
}
}
// onkeypress="javascript:return RestrictSpaceSpecial(event);"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment