How can I not skip anything other than numbers, periods or commas?

  • 0
    I have a regular expression /^\d+((\.\,>*\d+)?$/ , I put a "keypress" event handler on the input.
    When entering, do not skip anything other than numbers and a period or comma, but I cannot enter a note or a period.
    How to fix?
    JavaScript Anonymous, Jun 12, 2019

  • 3 Answers
  • 0
    /^\d+ [\.\,,?\d*$/
    Anonymous

  • 0
    Your expression says:


    1. must be at least one digit first;

    2. then - {comma or period zero or more}; and immediately after that - at least one digit.

    The second point is optional.



    Let's say you enter "2.71". After entering "2" - everything is fine. And after entering "." - the string does not match the pattern, because there is no digit after the dot yet.

    You can try typing "27" or "271" and then inserting a period.



    By the way, why do you allow many periods and commas?
    Mia Shepard

  • 0
    / ^ \ d + ([\. \,] d +)? $ /

    If you want to use positive numbers.

    1. Beginning of line

    2. At least one digit

    3. Optional: Period or comma followed by at least one digit

    4. End of line

    But this should be put on the validation of the entire line, and not on keypress

    On kepress it is necessary

    / ^ \ d + [\.,]? \ d + $ /
    Elizabeth Cook

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!