Skip to content

Instantly share code, notes, and snippets.

@fhdalikhan
Created January 29, 2019 05:50
Show Gist options
  • Save fhdalikhan/023f5459030ac3f3f903e1403b906bbe to your computer and use it in GitHub Desktop.
Save fhdalikhan/023f5459030ac3f3f903e1403b906bbe to your computer and use it in GitHub Desktop.
This javascript function can be used to only allow typing of alphabets, & _ - and whitespace, it will remove all other characters, it uses a regex for this, you can modify to allow other characters, for example to allow numbers as well just add 0-9 to the regex after A-Z.
(function(){
// Allow only alphabets & _ - whitespaces, strip out all others.
function clean(str) {
return str.replace(/[^a-z-A-Z\&\_\- ]/g, "").replace(/ +/, " ")
}
$('body').on('keyup', '#ask_message_textarea, #ask_message_title', function(event) {
this.value = clean(this.value);
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment