www.webdeveloper.com
Results 1 to 8 of 8

Thread: How do you limit type of characters entered in form field.

  1. #1
    Join Date
    Jul 2005
    Posts
    50

    How do you limit type of characters entered in form field.

    i.e: Only allow numbers in zip codes.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <label>Zip code<input onmouseup="this.value = this.value.replace(/\D/g, '')" type="text"></label>
    That'll work in I.E and Firefox.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Might wanna try onkeyup instead of onmouseup.

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Oops.

    But I'll have to post a comment over on the W3C's XHTML 2.0 developnment forum. Onmouseup might be a useful handler.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    What do you mean? It already is an event handler: [link].

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    I was thinking about a handler for when the mouse moves vertically.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    That is easy enough to detect for with some simple mathematics...

    But about that code you posted, that can be annoying if trying to press left to change the first characters, this is much less obtrusive:

    Code:
    <label>Zip code<input onkeyup="var v=this.value.replace(/\D/g, ''); if (this.value!=v) this.value=v" type="text"></label>
    Charles, I don't know regexp so could you please post the same regexp but for allowing numbers and the "-" sign?
    Last edited by BigMoosie; 08-28-2005 at 09:13 AM.

  8. #8
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <label>Zip code<input onkeyup="var v=this.value.replace(/[^\d\-]/g, ''); if (this.value!=v) this.value=v" type="text"></label>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles