I have a regular expression that will be matched against the keypress of the user. I'm quite stuck with it.
Here is my current code:
<script type="text/javascript">
$('input.alpha[$id=tb1]').keydown(function (e) {
//var k = e.which;
//var g = e.KeyCode;
var k = $(this).val();
//var c = String.fromCharCode(e.which);
if (k.value.match(/[^a-zA-Z0-9 ]/g)) {
e.preventDefault();
}
});
</script>
The goal here is to prevent the user from typing characters that are inside the regex.
k.value, whenk = $(this).val()? Have you checked for duplicates? I am sure there are many similar questions on SO. See this, this, this and many more..$(this).val()is input's value before key was pressed, that means you don't check last key pressed..