Fog Juice Posted July 6, 2011 Share Posted July 6, 2011 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');" /> Quote Link to comment https://forums.phpfreaks.com/topic/241177-regex-strangely-not-allowing-users-to-paste-in-textbox/ Share on other sites More sharing options...
gizmola Posted July 6, 2011 Share Posted July 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241177-regex-strangely-not-allowing-users-to-paste-in-textbox/#findComment-1238860 Share on other sites More sharing options...
Fog Juice Posted July 6, 2011 Author Share Posted July 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241177-regex-strangely-not-allowing-users-to-paste-in-textbox/#findComment-1238863 Share on other sites More sharing options...
gizmola Posted July 6, 2011 Share Posted July 6, 2011 It's because your first regex allows letters. So it accepts the 'v'. Quote Link to comment https://forums.phpfreaks.com/topic/241177-regex-strangely-not-allowing-users-to-paste-in-textbox/#findComment-1238878 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.