Jump to content

regex strangely not allowing users to paste in textbox?


Fog Juice

Recommended Posts

I have the following code below which allows only alphanumeric characters for one text box, and then only numeric characters in the second. My problem is that in the first box, users can paste, which is perfectly fine, but in the second text box, they can't paste. I'd like it so that users could paste in each text box. Does anyone know why the second text box doesn't allow users to paste? It is quite odd to me and I'm not sure why.

 

One other thing is that the first textbox allows the % sign, but as far as I can see, the regex I have is only for alphanumeric characters.


<script>
function alphanumericscan(e, call){
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
//alert(\'Character was \' + character);
    //alert(code);
    if (code == 8 || code == 9 || code == 37 || code == 39 ) return true;
if(call == "alpha") var AllowRegex  = /^[a-zA-Z0-9]+$/;
else var AllowRegex  = /^[0-9]+$/;

    if (AllowRegex.test(character)) return true;    
    return false;
}
</script>

Letters and numbers only: <input onkeypress="return alphanumericscan(event, 'alpha');" /><br />

Numbers only: <input onkeypress="return alphanumericscan(event, 'numonly');" />

Link to comment
Share on other sites

You are not detecting when the ctrl key is being pressed, so even though ctrl-v is a special combination, the keycode for v fails your numeric only regex, which defeats the paste functionality when using a key combination.  Unfortunately, if you allow this, you will bypass your key by key checking.  Even now you can see this is in action, by copying something that would not be legal into your paste buffer (try foo!!!!) and paste it into your text fields and you'll see that it bypasses your regex restriction.

Link to comment
Share on other sites

You are not detecting when the ctrl key is being pressed, so even though ctrl-v is a special combination, the keycode for v fails your numeric only regex, which defeats the paste functionality when using a key combination.  Unfortunately, if you allow this, you will bypass your key by key checking.  Even now you can see this is in action, by copying something that would not be legal into your paste buffer (try foo!!!!) and paste it into your text fields and you'll see that it bypasses your regex restriction.

 

What I don't understand though is why does pasting work in the first textbox, but not the second? They are both using the same functions, same key codes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.