Hitori Hitori - 7 months ago 9
HTML Question

How can I allow the user to input Numeric (with/without decimal point) only. OR numeric with 'K'/'B'

I've been searching through Stack Overflow, but I can't get the right answer to my question.

How can I allow the user to input a number or number with 'K (Thousands)' or 'M (Millions)'?

I've tried using patterns, RegExp, etc.

Is there a regular expression for this? I tried using

^[0-9]*$
and also
^[0-9\K\M]*$
, but I don't know if it is the correct expression.

Answer

Think you mean this,

document.write(/^\d+(?:\.\d+)?[KM]?$/.test('121K') + '<br/>');
document.write(/^\d+(?:\.\d+)?[KM]?$/.test('121.7M') + '<br/>');
document.write(/^\d+(?:\.\d+)?[KM]?$/.test('121.45') + '<br/>');

Comments