SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Dec 8, 2003, 09:16 #1
Enter Button expressed as regular expression
Hi,
I am trying to add the Enter button to the following condition:
allow="/[A-Za-z\s,.?!]/"
Does anyone know how to achieve it?
DHLast edited by dhtmlhelp; Dec 8, 2003 at 15:17.
-
Dec 8, 2003, 15:17 #2
Just pushing this thread up, hope someone can post a solution.
thanks,
DH
-
Dec 8, 2003, 15:34 #3
- Join Date
- Nov 2002
- Location
- Netherlands
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There is no way to match an enter with a regular expression, since regular expressions are made to match string patterns, not keyboard input. (I know that is what you use it for, but still).
Your best bet would be to check the keycode of the special keys, before you check against the regular expression. The enter key is most likely keycode 13 (I havent checked, but you can easily check by printing the code to the screen).
-
Dec 8, 2003, 15:40 #4
Hi TSS,
could you please clarify what you mean by
... Your best bet would be to check the keycode of the special keys, before you check against the regular expression.
... printing the code to the screen.
-
Dec 9, 2003, 06:52 #5
- Join Date
- Nov 2002
- Location
- Netherlands
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, look at the following line out the code you are using:
Code:if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
Code:alert ("Code for this key is: " + event.keyCode);
Ofcourse you can make both expressions into one IF-statement as well, it's all about what you want to do with it.
-
Dec 9, 2003, 08:54 #6
- Join Date
- Dec 2003
- Location
- A van down by the river
- Posts
- 2,056
- Mentioned
- 0 Post(s)
- Tagged
- 1 Thread(s)
It's true that there will be no 'enter key character' in the string from a textbox and you will need to resort to event processing, but if you are capturing text from a textarea, a newline character (\n) can be detected in the string.
-
Dec 9, 2003, 11:10 #7
- Join Date
- Nov 2002
- Location
- Netherlands
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is a followup on one of his earlier posts in this javascript section. He wanted to allow certain keys to be pressed (and others get their event cancelled) by checking against a regular expression. Take a look at:
http://www.sitepointforums.com/showthread.php?t=142118
-
Dec 9, 2003, 15:13 #8
Thanks TSS,
quite interesting, didn't expect you to be around, hence the new post. It is 13 as you suggested. So do I have to do the following now?:
Code:if (!String.fromCharCode(13)) event.returnValue=true; if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
DH
EDIT: nope, gives errors, should I be using an elseif?Last edited by dhtmlhelp; Dec 9, 2003 at 16:01.
-
Dec 9, 2003, 19:55 #9
- Join Date
- Nov 2002
- Location
- Netherlands
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sure as hell gives errors
I would think something along the lines of the following should work:
Code:if (event.keyCode == 13) { // Do something alert ("You pressed the enter key, naughty boy"); } else { // Check for valid input, if invalid, cancel this event. if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false; }
-
Dec 10, 2003, 04:08 #10
HI TSS,
thanks but I want the user to be able to press the enter button as a general rule. I have tried the following:
Code:function filterInput(e) { // Get the regular expression to test against for this particular object regAllow = (e)?eval(e.allow):eval(event.srcElement.allow); if (event.keyCode == 13) { // Do something event.returnValue=true; } else { // Check for valid input, if invalid, cancel this event. if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false; }
why?
DH
-
Dec 10, 2003, 04:54 #11
- Join Date
- Nov 2002
- Location
- Netherlands
- Posts
- 165
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This works fine for me:
Code:<script> function filterInput(e) { // Get the regular expression to test against for this particular object regAllow = (e)?eval(e.allow):eval(event.srcElement.allow); // Check for an allowed character, if not found, cancel the keypress's event bubbling if (event.keyCode == 13) { // Do nothing, i.e. allow. } else { if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false; } } </script>
-
Dec 10, 2003, 05:32 #12
Gotcha, thanks TSS, going to study some more javascript I think.
DH
Bookmarks