event.keycodeFirefox was not supported. Use event.whichfor firefox. Check the code below, which allows only alphanumeric and hyphen.
$(function() {
$('#slug_input').keypress(function(evt) {
var keyID = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
return ((keyID >= 97 && keyID <= 122) || (keyID >= 48 && keyID <= 57) || keyID == 45);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="slug_input">
Run codeHide resultBut, as the tushar suggested, I suggest you use Regex matching.
source
share