SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    DH
    Last edited by dhtmlhelp; Dec 8, 2003 at 15:17.

  2. #2
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just pushing this thread up, hope someone can post a solution.

    thanks,

    DH

  3. #3
    SitePoint Zealot tss68nl's Avatar
    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).

  4. #4
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    and by

    ... printing the code to the screen.
    DH

  5. #5
    SitePoint Zealot tss68nl's Avatar
    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;
    It uses the event.keyCode, which ofcourse you can check or print directly as well. For example, you could insert an alert in front of the if statement reading:

    Code:
    alert ("Code for this key is: " + event.keyCode);
    Once you know the code for the enter button (probably 13, but you can check with the above function), you can then put that in an if statement as well. Possible constructs would be to first check if the keyCode = 13, if it is, just let it pass through, and else, check with the regular expression.

    Ofcourse you can make both expressions into one IF-statement as well, it's all about what you want to do with it.

  6. #6
    SitePoint Wizard silver trophy rushiku's Avatar
    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.

  7. #7
    SitePoint Zealot tss68nl's Avatar
    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

  8. #8
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;
    It seems to work, is it the correct way though?

    DH

    EDIT: nope, gives errors, should I be using an elseif?
    Last edited by dhtmlhelp; Dec 9, 2003 at 16:01.

  9. #9
    SitePoint Zealot tss68nl's Avatar
    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;
    }

  10. #10
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;
    	}
    but now it allows all characters...

    why?

    DH

  11. #11
    SitePoint Zealot tss68nl's Avatar
    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>
    You never have to set the events returnvalue to true, since it bubbles up by default anyways. You only have to cancel when you recieve input you dont want.

  12. #12
    SitePoint Evangelist dhtmlhelp's Avatar
    Join Date
    May 2003
    Posts
    575
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gotcha, thanks TSS, going to study some more javascript I think.

    DH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •