skip to Main Content
oninput="this.value = this.value.replace(/[^-0-9.]/g, '') 

I use this code in my project, but input also can get more than 1 minus sign like this:

---23

I want my input have this conditions:

  1. only can get positive and negative numbers
  2. zero or one minus

2

Answers


  1. Chosen as BEST ANSWER

    .replace(/^(-)+|[^d.]/g, '$1')

    this is the right answer.


  2. You can adjust your regex to below

    EDIT.

    Based on your requirements:

    • positive or negative integers
    /^(-)?[0-9]*/g
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search