Forum Moderators: open
Using my code it seems to validate properly if only non-allowable characters exist, as soon as you include allowable and non-allowable content it no longer validates properly. This is my first time using regular expression so I'm sure I'm missing something.
<script type="text/javascript" language="JavaScript">
function checkComment()
{
var validcontent = /[\w\s\.\,\?]/;
if (!validcontent.test(document.getElementById('comment').value)) {
alert("Your Comment field can only contain numbers, letters, periods, question marks and spaces");
document.getElementById('comment').focus();
return false;
} else {
document.getElementById('form1').submit();
}
}
</script>
<form method="POST" action="#" name="form1" >
<textarea cols="20" rows="5" name="comment" id="comment"></textarea>
<input type="button" name="submitBtn" value="send" onClick="checkComment();">
</form>
what you probably want is something like this:
...
var invalidcontent = /[^\w\s\.\,\?]/;if (invalidcontent.test(document.getElementById('comment').value)) {
...
the [^...] construct in a regular expression means "not these characters".