SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    event in Mozilla

    I've writtena script that traps for characters I don't want a user to be able to enter into a form. It works fine in Internet Explorer but Mozilla throws up an error about "event is not defined". Any ideas how to get round this ?

    My code is
    HTML Code:
    function validCharacters()
    {
    	var prohibited = "(,|\\\":<>`';&%)";
    	if (prohibited.indexOf(String.fromCharCode(event.keyCode)) >= 0) 
    	{
    		alert ('The following characters are not permitted in this field\n\n'+prohibited');
    		return false;
    	}
    }

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    function validCharacters(evt)
    {
      var e = evt || window.event;
      var prohibited = "(,|\\\":<>`';&%)";
      if (prohibited.indexOf(String.fromCharCode(e.keyCode)) >= 0) 
      {
        alert ('The following characters are not permitted in this field\n\n'+prohibited');
        return false;
      }
    }

  3. #3
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Netscape and Mozilla use event.which instead of event.keyCode. Opera uses event.keyCode, so the code below is cross-browser as far as the 3 of 'em.

    HTML Code:
    <html>
    <head>
    <title>Cross-Browser Keypress Events</title>
    <script type="text/javascript">
    function validCharacters(evt)
    {
    	var keyCode = evt.which ? evt.which : evt.keyCode;
    	var prohibited = "(,|\\\":<>`';&%)";
    	if (prohibited.indexOf(String.fromCharCode(keyCode)) >= 0)
    	{
    		alert ('The following characters are not permitted in this field\n\n'+prohibited);
    		return false;
    	}
    }
    </script>
    </head>
    <body>
    <form>
    Input: <input onkeypress="return validCharacters(event);"
    	type="text" name="t" />
    </form>
    </body>
    </html>
    -xDev

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    xDev is right. I didn't answer your question well enough. The following is from one of my dhtml libraries:
    Code:
    function xEvent(evt) { // cross-browser event object prototype
      this.type = '';
      this.target = null;
      this.pageX = 0;
      this.pageY = 0;
      this.offsetX = 0;
      this.offsetY = 0;
      this.keyCode = 0;
      var e = evt ? evt : window.event;
      if(!e) return;
      if(e.type) this.type = e.type;
      if(e.target) this.target = e.target;
      else if(e.srcElement) this.target = e.srcElement;
      else if(xNN4) this.target = xLayerFromPoint(e.pageX, e.pageY);
      if(xOp5or6) { this.pageX = e.clientX; this.pageY = e.clientY; }
      else if(xDef(e.pageX,e.pageY)) { this.pageX = e.pageX; this.pageY = e.pageY; } // v3.14
      else if(xDef(e.clientX,e.clientY)) { this.pageX = e.clientX + xScrollLeft(); this.pageY = e.clientY + xScrollTop(); }
      if(xDef(e.offsetX,e.offsetY)) { this.offsetX = e.offsetX; this.offsetY = e.offsetY; }
      else if(xDef(e.layerX,e.layerY)) { this.offsetX = e.layerX; this.offsetY = e.layerY; }
      else { this.offsetX = this.pageX - xPageX(this.target); this.offsetY = this.pageY - xPageY(this.target); }
      if (e.keyCode) { this.keyCode = e.keyCode; } // for moz/fb, if keyCode==0 use which
      else if (xDef(e.which)) { this.keyCode = e.which; }
    }
    In particular let's look at the last two lines:
    Code:
      ...
      if (e.keyCode) { this.keyCode = e.keyCode; } // for moz/fb, if keyCode==0 use which
      else if (xDef(e.which)) { this.keyCode = e.which; }
    }
    I found that for Moz/Fb keyCode is valid - but if it is zero then you have to use which. For some previous versions of Moz this was not true - you always used which. They like to keep us on our toes

    (btw, the function xDef just returns true if all its arguments are 'defined')

    Obviously I need to do more testing on this but... it's discouraging because browser object models change so often (and sometimes drastically) between each version release. And of course these kinds of changes aren't documented very well, if at all.

  5. #5
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the excellent responses. It is really useful when not only do you solve the problem but explain what is going on. I have modified the code and it works like a charm. Rated you both accordingly

    thanks again


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
  •