英文:
Dont allow repeat two commas two dots regex javascript
问题
我想禁止输入超过一个句点,同时我想禁止输入超过一个逗号
如果已经有一个句点(.),则不应允许输入其他句点(.)或逗号(,),反之亦然
我有这个示例:
function isNumberKey(evt, obj) {
if (evt.target.selectionStart === 0 && (evt.keyCode == 46)) return false;
var charCode = (evt.which) ? evt.which : evt.keyCode
var value = obj.value;
var dotcontains = value.indexOf(".") != -1;
var commacontains = value.indexOf(",") != -1;
if ((value.length == 0) && (event.keyCode == 46)) return false
if (dotcontains || commacontains)
if (charCode == 46 || charCode == 44) return false;
if (charCode == 46 || charCode == 44) return true;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
那个代码有效,但我需要使用正则表达式来做到这一点。
我尝试了这个正则表达式:
/^[0-9]+(?:\.[0-9]+)*$/
它应该像这样工作:
<input type="number" />
英文:
I want to forbiden to write more that one dot, as well i want to forbiden to write more that one comma
if there is already an dot (.) should not allow to write other dot(.) or comma (,) and viceversa
i have this example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function isNumberKey(evt, obj) {
if (evt.target.selectionStart === 0 && (evt.keyCode == 46)) return false;
var charCode = (evt.which) ? evt.which : evt.keyCode
var value = obj.value;
var dotcontains = value.indexOf(".") != -1;
var commacontains = value.indexOf(",") != -1;
if ((value.length == 0) && (event.keyCode == 46)) return false
if (dotcontains || commacontains)
if (charCode == 46 || charCode == 44) return false;
if (charCode == 46 || charCode == 44) return true;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
<!-- language: lang-html -->
<INPUT TYPE="TEXT" VALUE="0" onkeypress="return isNumberKey(event,this)">
<!-- end snippet -->
that is working but i need to do that with regex
i was trying with this regex:
/^[0-9]+(?:\.[0-9]+)*$/
it should work like
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<input type="number" />
<!-- end snippet -->
答案1
得分: 1
你可以尝试这个正则表达式:
[^\d.,]|(^\d*[.,]\d*).*
然后用$1
来替换它。
基本上它只允许数字字符、,
和.
,并且只允许一个,
或.
存在,其他任何字符都会在匹配时被替换为空。
编辑
根据评论中添加的信息,如果要只允许最多3位小数,你可以稍微修改正则表达式:
[^\d.,]|(^\d*[.,]\d{0,3}).*
编辑 2
如果要允许的数字只在1百万以下,你可以限制最多有效数字为6位,并将小数部分变为可选项:
[^\d.,]|(^\d{0,6}(?:[.,]\d{0,3})?).*
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
const $input = document.querySelector('input');
$input.addEventListener('input', () => {
$input.value = $input.value.replace(/[^\d.,]|(^\d{0,6}(?:[.,]\d{0,3})?).*/g, '$1');
});
<!-- 语言: lang-html -->
<input />
<!-- 结束代码片段 -->
英文:
You may try this regex
[^\d.,]|(^\d*[.,]\d*).*
and replace it with $1
.
Basically it only allows numeric characters, ,
and .
, and it only allows one ,
or .
to be present, anything other than those will be replaced to empty on the fly.
Edit
According to the added information in comments, to only allow maximum 3 fraction digits, you may change the regex a little bit:
[^\d.,]|(^\d*[.,]\d{0,3}).*
Edit 2
To allow numbers only lower than 1 million, you can restrict the maximum significant digits to 6, and make the fraction part optional:
[^\d.,]|(^\d{0,6}(?:[.,]\d{0,3})?).*
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const $input = document.querySelector('input');
$input.addEventListener('input', () => {
$input.value = $input.value.replace(/[^\d.,]|(^\d{0,6}(?:[.,]\d{0,3})?).*/g, '$1');
});
<!-- language: lang-html -->
<input />
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论