不允许重复的两个逗号两个点正则表达式 JavaScript。

huangapple go评论55阅读模式
英文:

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 &amp;&amp; (evt.keyCode == 46)) return false;
  var charCode = (evt.which) ? evt.which : evt.keyCode
  var value = obj.value;
  var dotcontains = value.indexOf(&quot;.&quot;) != -1;
  var commacontains = value.indexOf(&quot;,&quot;) != -1;
  if ((value.length == 0) &amp;&amp; (event.keyCode == 46)) return false
  if (dotcontains || commacontains)
    if (charCode == 46 || charCode == 44) return false;

  if (charCode == 46 || charCode == 44) return true;
  if (charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 57))
    return false;
  return true;
}

<!-- language: lang-html -->

&lt;INPUT TYPE=&quot;TEXT&quot; VALUE=&quot;0&quot; onkeypress=&quot;return isNumberKey(event,this)&quot;&gt;

<!-- 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 -->

&lt;input type=&quot;number&quot; /&gt;

<!-- 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(&#39;input&#39;);

$input.addEventListener(&#39;input&#39;, () =&gt; {
  $input.value = $input.value.replace(/[^\d.,]|(^\d{0,6}(?:[.,]\d{0,3})?).*/g, &#39;$1&#39;);
});

<!-- language: lang-html -->

&lt;input /&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 01:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定