I have a search box, which restricts special characters. I have written code for that also. The problem is that when I type search and press enter it doesn't works. Please see the code for your reference
<script type="text/javascript">
$(document).ready(function (){
$('#ctl00_topNavigation_txtSearch').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9\b _ _%]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
if (event.which == 13 || event.keyCode == 13) {
window.location.href = "search.aspx?tx=" + txtSearch.Text;
return false;
}
return true;
});
});
</script>
The regex validation part is working fine, but it doesn't enables on key press of enter. Let me know the issue, where I am mistaking in the code.