user990635
user990635

Reputation: 4259

Preventing user from inserting *

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);" />

Upvotes: 0

Views: 234

Answers (4)

Martin Schaer
Martin Schaer

Reputation: 4006

This should prevent people typing * and copy and pasting it to. Remember to check on the back-end to, because people may have JS disabled in their browsers.

Javascript:

function testField(f)
{
    var o='',i,v=f.value;
    for(i=0;i<v.length;i++) if(v[i]!='*')o+=v[i];
    f.value=o;
}

HTML:

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

DEMO: http://jsfiddle.net/martinschaer/Sbg6w/ (manually optimized and minified ;))

EDIT: Using a flag (c) you can avoid having the caret positioned at the end if you try to edit what you've typed so far. Like this:

function testField(f)
{
    var t,c,i=0,o='',x="value",v=f[x],l=v.length;
    while(i<l){t=v[i++];if(t!='*')o+=t;else c=1}
    if(c)f[x]=o
}

​Test it here: http://jsfiddle.net/martinschaer/Sbg6w/

Upvotes: 0

jchook
jchook

Reputation: 7260

<script type="text/javascript">
function testField(field, event) {
    if (event.charCode == 42) {
        event.preventDefault();
    }    
}
</script>

<input type="text" id="searchGamesKeyword" class="searchGamesTextBox"
name="searchGamesKeyword" onkeypress="javascript:testField(this, event);" />​

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150078

How about this:

function testField(field) {
    field.value = field.value.replace(/\*/g,"");
}

Called from onblur=testField(this) as shown in the question it will take the current value of the field and replace any and all asterisks with an empty string, leaving all other characters untouched. So, e.g., "ab*cde*" would become "abcde".

The g on the end of /\*/g is a flag meaning to match globally - without this flag it would just replace the first match.

The reason your code didn't work is that your regex of [^*] will match (i.e., return true from .test()) if there is a non-asterisk character anywhere in the string.

Upvotes: 1

sarnold
sarnold

Reputation: 104090

You forgot to anchor your regexp to the start and end of the string: new RegExp("[^*]")

Try this: var regExpr = /^[^*]*$/ -- it asks for zero or more instances of any character except * anchored at the start and end of the string. Maybe /^[^*]+$/ would be better (note +) if you want to force one or more instances of any character except *.

Upvotes: 2

Related Questions