Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only positive and negative numbers in Javascript regex with zero or one minus symbol

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 signs like this:

---23

I want my input to have these conditions:

  1. only can get positive and negative numbers
  2. zero or one minus
like image 215
ali_he96 Avatar asked Oct 29 '25 14:10

ali_he96


1 Answers

You can adjust your regex to below

EDIT.

Based on your requirements:

  • positive or negative integers
/^(-)?[0-9]*/g
like image 77
Jeremy Fiel Avatar answered Oct 31 '25 09:10

Jeremy Fiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!