Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th and so on :(
Script :
<script language="javascript" type="text/javascript">
var reg = /[^\w]/gi;
function checkNonWordChars() {
var str = $("#TestTextbox").val();
if (reg.test(str)) {
alert('!!! Non-Word Char Exists !!!');
}
else {
alert('input accepted');
}
}
</script>
HTML :
<input type="text" id="TestTextbox" />
<input type="button" value="Test" onclick="checkNonWordChars();" />
If I click on button once, it will fire an alert saying that “!!! Non-Word Char Exists !!!” but if I click it again, it will fire an alert saying “input accepted” :(