134
rated 0 times
[
135]
[
1]
/ answers: 1 / hits: 17781
/ 11 Years ago, fri, january 10, 2014, 12:00:00
I am using a AngularJS directive to only allow a user to enter numbers, decimals, and minus signs however I'd like to update it to allow only one dot and one negative sign at the beginning. The following is what I have now but it allows invalid inputs like --1
:
val.replace( /[^0-9.-]+/g, '');
I need help fixing the RegEx to allow inputs like -1
, .1
, -.1
, 0.1
, and -0.1
but not like --1
, 1.1.1
, 1-1
, -.
, 1.
, 1.k
, and k
.
I managed to only allow one decimal with the following but have struggled with then allowing the negative sign only at the beginning:
val.replace(/[^d*[0-9].d*[0-9]]/g, '');
More From » regex