Last active
November 11, 2019 16:24
-
-
Save PiotrKrzyzek/f28ea20c880b71ec3c51523caaf38ea3 to your computer and use it in GitHub Desktop.
Restrict special characters with javascript / jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
$(document).ready(function() { | |
$('textarea').bind('keypress', function (event) { | |
// Allow for regular characters and include these special few: | |
// comma, period, explanation point, new line | |
var regex = new RegExp("^[a-zA-Z0-9\r\n\.\,\!\ ]+$"); | |
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); | |
if (!regex.test(key)) { | |
event.preventDefault(); | |
return false; | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment