[英]How can I allow the user to input Numeric (with/without decimal point) only. OR numeric with 'K'/'B'
我一直在搜索Stack Overflow,但我無法得到正確的答案。
如何允許用戶輸入帶有“K(千位)”或“M(百萬位)”的數字或數字?
我嘗試過使用模式,RegExp等。
這有正則表達式嗎? 我嘗試使用^[0-9]*$
和^[0-9\\K\\M]*$
,但我不知道它是否是正確的表達式。
這會奏效
^(\d+(?:\.\d+)?)\s*([KMB])?$
JS代碼
var re = /^(\\d+(?:\\.\\d+)?)\\s*([KMB])?$/; var str = '1234.567K'; var result = str.match(re); document.writeln(result + '<br>')
認為你的意思是,
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/>');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.