0

I need to alter users input and leave in the box only integer or decimal values, i.e. 4567 or 354.5635. I use the following statement:

v = v.replace(/[^\d\.]+/g, "");

But this allows multiple decimal points such as 345.45.345.67. How do I ensure that only one point is there?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Kizz
  • 779
  • 1
  • 10
  • 15

5 Answers5

4

v = parseFloat(v).toFixed(numberOfDecimalDigits);

Ernest
  • 570
  • 3
  • 6
  • parseFloat() is my favourite way, but be sure your float is small enough because parseFloat does not work well with high precision numbers like parseFloat('1.0000000000000001') – Mike Kovetsky Feb 11 '22 at 13:40
2

Not sure if JS can do lookahead assertions, but if it can it could be done with a regex IF JS can also do reverse(input_string).

A reverse is needed because you want to allow the FIRST dot, and take out the rest.
However, search progresses left to right.

/(?:[.](?=.*[.])|[^\d.])+/g will take out all but the last '.'
So, the string has to be reversed, substituted, then reversed again.

Perl code example:

my $ss = 'asdf45.980.765';
$ss = reverse $ss;
$ss =~ s/(?:[.](?=.*[.])|[^\d.])+//g;
$ss = reverse $ss;
print $ss;

Output: 45.980765

1
v = v.replace(/[A-Za-z$-]/g, ""); 
     // This will replace the alphabets

v = v.replace(/[^\d]*(\d+(\.\d+)?)?.*/g, "$1"); 
    // This will replace other then decimal
Shreya
  • 21
  • 4
0

I'd recommend to use parseFloat here:

v = parseFloat(v);

This will guarantee you to have integer/decimal value or NaN.

yko
  • 2,710
  • 13
  • 15
  • Thanks @TomalakGeret'kal I misunderstood question a bit – yko Nov 15 '11 at 16:42
  • No problem. :) [I could continue, by pointing out that decimal base is orthogonal from floating-point representation (and, if anything, "binary" would be appropriate), but I shan't...] – Lightness Races in Orbit Nov 15 '11 at 16:55
  • The project owner wants to replace all "wrong" input "on-the-fly" using txt's onblur and onkeyup handlers. I guess I either need to add some complicated logic to supplement that initial regex or I need to convince him to opt for a typical input validation and use parsing to test if the input is numeric. – Kizz Nov 15 '11 at 16:56
  • 2
    @Kizz, you better add input validation during the input, but instead of editing user input, highlight error. For example, make `$(input).css('background','red')` when something wrong entered. – yko Nov 15 '11 at 16:58
-1
v = v.replace(/[^\d]*(\d+(\.\d+)?)?.*/g, "$1");
Eugene
  • 3,280
  • 21
  • 17
  • This doesn't work. It even allows letters. The idea of my expression is not to allow anything except for digits and a dot. Hense the ^ char. Thanks though. – Kizz Nov 15 '11 at 16:37
  • Works for me: var v = ' 345.45.345.67'; v = v.replace(/[^\d]*(\d+(\.\d+)?).*/g, "$1"); console.log(v); // Outputs 345.45 – Eugene Nov 15 '11 at 16:43
  • @Eugene: That's hardly a rigorous test. A breaking test is really easy to come up with: feed `"a"` to your regular expression. – Lightness Races in Orbit Nov 15 '11 at 16:50
  • Let's say that the user inputs the letter "H". I need to disallow that, allowing only integers and an optional singledot. Your expression doesn't do that: txt.value = txt.value.replace(/[^\d]*(\d+(\.\d+)?).*/g, ""); If the txt.value is "H", it stays "H", nothing gets replaced. – Kizz Nov 15 '11 at 16:51
  • 1
    Choosing regular expressions for the task at hand is wrong, and this little trail of disasters is precisely why. – Lightness Races in Orbit Nov 15 '11 at 16:56
  • But how do you sanitize that input if it starts with a letter without regular expression? – Eugene Nov 15 '11 at 17:03
  • @Tomalak: I think you are right. I'd probably switch to normal input validation on submit. – Kizz Nov 15 '11 at 17:06
  • @Eugene: yko showed you half an hour ago: Javascript has had numeric parsing built-in for decades. – Lightness Races in Orbit Nov 15 '11 at 17:08
  • var v = 'a123.12'; v = parseFloat(v); console.log(v); // Output - NaN – Eugene Nov 15 '11 at 17:09
  • @Eugene: ... which is correct. Last I checked, `a123.12` is not a valid number. Do correct me if I'm wrong. – Lightness Races in Orbit Nov 15 '11 at 18:45
  • Quote - "I need to alter users input and leave in the box only integer or decimal values, i.e.". We are talking about sanitizing user's input. "built-in numeric parsing" won't do the job. – Eugene Nov 15 '11 at 21:43