Regular expression for carriage return/enter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stevewy@hotmail.com

    Regular expression for carriage return/enter?


    If the following line will force the user to input only a number
    between 1 and 5, and will give an error to any keypress outside that
    range:

    onkeyup="if(eve nt.srcElement.v alue.search(new RegExp('^[1-5]$')))
    {alert('Please type in a number between 1 and
    5');event.srcEl ement.value=''; return false}"

    .... how could I get the regular expression to accept 1 to 5 *or* the
    Enter/Return keypress? I have tried ('^[1-5][\r]$') but that doesn't
    work. Is there a syntax for saying "1 to 5 or Enter/Return"?

    Thank you for any info you can give.

    Steve
  • stevewy@hotmail.com

    #2
    Re: Regular expression for carriage return/enter?

    Ah - figured it out: I'm barking up the wrong tree because the
    "value" of the source element will never "equal" a Return character.
    I needed to do: (new RegExp('^[1-5]$'))&&event.key Code!=13)

    Well, hopefully this post may be of use to anyone else looking for a
    similar solution.

    Steve

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Regular expression for carriage return/enter?

      stevewy@hotmail .com wrote:
      If the following line will force the user to input only a number
      between 1 and 5, and will give an error to any keypress outside that
      range:
      >
      onkeyup="if(eve nt.srcElement.v alue.search(new RegExp('^[1-5]$')))
      {alert('Please type in a number between 1 and
      5');event.srcEl ement.value=''; return false}"
      This is error-prone, incompatible, illegible, and inefficient. You were
      looking for

      maxlength="1"
      onkeyup="if (!/[1-5]/.test(this.valu e))
      {
      window.alert('P lease type in a number between 1 and 5');
      this.value = '';
      return false;
      }"

      instead. It is still awful. For example, I have just switched from that
      focused control to the newsreader with Alt+Tab and got the error message.


      PointedEars
      --
      realism: HTML 4.01 Strict
      evangelism: XHTML 1.0 Strict
      madness: XHTML 1.1 as application/xhtml+xml
      -- Bjoern Hoehrmann

      Comment

      • Bart Van der Donck

        #4
        Re: Regular expression for carriage return/enter?

        stev...@hotmail .com wrote:
        I'm barking up the wrong tree because the "value" of the source
        element will never "equal" a Return character.
        I needed to do: (new RegExp('^[1-5]$'))&&event.key Code!=13)
        Take this code Ad Unicem and it might not work anymore; the Enter-key
        can give keycode 10 too.

        10 = Carriage Return, as described in your subject line
        13 = Newline, the most common keycode for the Enter-key AFAIK

        ... && event.keyCode != 10 && event.keyCode != 13

        --
        Bart

        Comment

        Working...