Hitori Hitori - 1 year ago 46
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

and also
, but I don't know if it is the correct expression.

Answer Source

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/>');