Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Restricted the input field

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #42488
    kiddo
    Participant

    hi,

    i need to restrict my input field so it can only be filled by number, i manage to restricted the input field [Example](http://codepen.io/zhepyr/pen/Apnxd “Code pen”)

    but it didn’t work like i want, all i want is to prevent user to type/input alphabet into the field.

    ohh btw, if it’s possible i want to use only javascript not jquery

    #123393
    Bert
    Participant

    Hi,
    HTML5 can do this: http://www.w3schools.com/html/html5_form_input_types.asp
    And then validating the field after a submit and respond accordingly for browsers that don’t support this (yet).
    Just like you would do to check if a valid email address was entered.
    HTH

    #105681
    kiddo
    Participant

    yeah i tried that before but i doesn’t compatible with some browser, and about validate the field when submit i already do that
    but i think it’s more easier for user if we already restricted the input field just for number only.

    #105684
    Bert
    Participant

    Ok. Then jQuery:

    $(‘.numbers’).keypress(function(key) {
    if(key.charCode < 48 || key.charCode > 57) return false;
    });

    #105685
    Bert
    Participant

    Formatting on CSS-tricks is a bit tricky sorry:
    HTML:

    jQuery:
    $(‘.numbers’).keypress(function(key) {
    if(key.charCode < 48 || key.charCode > 57) return false;
    });

    #105686
    Bert
    Participant

    Sorry again.
    Look at this: http://codepen.io/begetolo/pen/jvldz

    #123394
    Bert
    Participant
    #123400
    kiddo
    Participant

    @Bert nice,that’s what i was looking for
    thank you very much.

    #123430
    Bert
    Participant

    You’re welcome. Glad I could help.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.