Preventing user from inserting *

Posted by user990635 on Stack Overflow See other posts from Stack Overflow or by user990635
Published on 2012-06-21T02:47:25Z Indexed on 2012/06/21 3:16 UTC
Read the original article Hit count: 90

Filed under:
|

I'm trying to prevent user from inserting * in a textbox.
This is what I was trying to do, but here it only detects * if this is the only inserted character. For example texts like: *, etc.
When allowed characters are mixed with *, then it cannot detect it. For example inputs such as: *hjh, etc..
and maybe how to make it replace only * with "" and not the whole field?

<script type="text/javascript">
    function testField(field) {
        var regExpr = new RegExp("[^*]");
        if(!regExpr.test(field.value)) {
            field.value = "";
        }
    }
</script>

<input type="text" id="searchGamesKeyword" class="searchGamesTextBox"
name="searchGamesKeyword" onblur="testField(this);" />

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html