Skip to content

Instantly share code, notes, and snippets.

@nadeem-khan
Created October 28, 2014 14:28
Show Gist options
  • Save nadeem-khan/894c1929049545f7a489 to your computer and use it in GitHub Desktop.
Save nadeem-khan/894c1929049545f7a489 to your computer and use it in GitHub Desktop.
Allow only alphanumeric characters in text input field with Jquery
$(function() {
$('#sidebar-filters-submit').click(function(e) {
$(".navigation").find(".error").remove();
allOk = true;
if ($("#search_page_keywords").val()) {
var input = $("#search_page_keywords").val();
if (/^[a-zA-Z0-9 ]*$/.test(input) === false) {
$("<label for=\"search_keyword_input\" class=\"search_keyword_input error\"> Your search string contains illegal characters.</label>").insertAfter("#search_page_keywords");
allOk = false;
}
if (!allOk) {
e.preventDefault();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment