Restrict to typing numbers only

Discussion in 'JavaScript' started by enchance, Sep 17, 2007.

  1. #1
    How do I make it so the input field only displays only numbers and not some other character? Like when someone types in a letter nothing comes out in the field but when it's a number it immediately gets displayed.
     
    enchance, Sep 17, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <input type="text" onkeyup="this.value = this.value.replace(/[^\d]/, '');" />
    
    HTML:
    Untested but should work.
     
    nico_swd, Sep 17, 2007 IP
  3. hemlata

    hemlata Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello enchance,

    The solution is..

    Regards.
     
    hemlata, Sep 17, 2007 IP
  4. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot. I just got a charcode converter for the other fields. This is really useful stuff.
     
    enchance, Sep 17, 2007 IP