防止将重复的逗号输入到输入文本框中

时间:2022-08-25 19:25:27

I'm using a textbox to collect numbers only, separated by a comma. No other characters, not even spaces, are allowed. Only 0-9 and a comma.

我正在使用文本框来收集数字,用逗号分隔。不允许使用其他字符,甚至空格。只有0-9和一个逗号。

The following function serves my purpose, but I'd like to now prevent a duplicate comma from being entered.

以下功能符合我的目的,但我现在要防止输入重复的逗号。

Correct e.g. 22,444,2,444

正确的,例如22,444,2,444

Incorrect e.g. 22,,444,2,444 OR 22,,444,,2,,444

例如不正确22,444,2,444或22,444,2,444

Here is the code I'm using for limiting the field to numbers, and allowing only a comma:

这是我用于将字段限制为数字的代码,并且只允许使用逗号:

$(document).ready(function(){
$("#customPrices").keypress(function (e) {


if (e.which != 8 && e.which != 0 && String.fromCharCode(e.which) != ',' && (e.which < 48 || e.which > 57))
{

//display error message
if($( "#errmsg" ).is(':hidden'))
{
$( "#errmsg" ).fadeIn();
}
return false;

}

});
});

I've found something that may be able to work at the following link: Javascript Help - prevent duplicate characters in textbox

我找到了一些可以在以下链接中工作的东西:Javascript帮助 - 防止文本框中出现重复字符

The example there needs to prevent a hyphen, which is charCode 45. A comma is charCode 44....

这个例子需要阻止连字符,即charCode 45.逗号是charCode 44 ....

But I'm not sure of how to implement it into my current code...

但是我不确定如何将它实现到我当前的代码中......

Any help would be really loved, thank you!

任何帮助都会真的很受欢迎,谢谢!

2 个解决方案

#1


1  

In your position, I would not try to validate the key press, but rather validate the content of the text box when the user changes it.

在您的位置,我不会尝试验证按键,而是在用户更改时验证文本框的内容。

This can e.g. be achieved using a regular expression:

这可以是例如使用正则表达式实现:

function validate() {
  var input = document.getElementById('inputField').value;

  if(input.match(/,{2,}/g)) {
    // the user entered a duplicate comma
    alert('No duplicate commas allowed!');
  }
}
<input oninput='validate()' id='inputField' />


You can also extend the regular expression to check the input for characters other than the numbers 0 to 9 and commas:

您还可以扩展正则表达式以检查除数字0到9和逗号之外的字符的输入:

/(,{2,}|[^,0-9])/g

This will also report e.g. 22,44,d,17 as invalid.

这也将报告,例如22,44,d,17为无效。

#2


1  

add the below condition:
1) take the existing value
2) check the last char is (,) and current char also (,) and validate
 var customPrices = $("#customPrices").val();

String.fromCharCode(e.which) == ',' && String.fromCharCode(e.which) == 
customPrices.charAt(customPrices.length-1)

or 
String.fromCharCode(e.which) == ',' && customPrices.charAt(customPrices.length-1) == ','

#1


1  

In your position, I would not try to validate the key press, but rather validate the content of the text box when the user changes it.

在您的位置,我不会尝试验证按键,而是在用户更改时验证文本框的内容。

This can e.g. be achieved using a regular expression:

这可以是例如使用正则表达式实现:

function validate() {
  var input = document.getElementById('inputField').value;

  if(input.match(/,{2,}/g)) {
    // the user entered a duplicate comma
    alert('No duplicate commas allowed!');
  }
}
<input oninput='validate()' id='inputField' />


You can also extend the regular expression to check the input for characters other than the numbers 0 to 9 and commas:

您还可以扩展正则表达式以检查除数字0到9和逗号之外的字符的输入:

/(,{2,}|[^,0-9])/g

This will also report e.g. 22,44,d,17 as invalid.

这也将报告,例如22,44,d,17为无效。

#2


1  

add the below condition:
1) take the existing value
2) check the last char is (,) and current char also (,) and validate
 var customPrices = $("#customPrices").val();

String.fromCharCode(e.which) == ',' && String.fromCharCode(e.which) == 
customPrices.charAt(customPrices.length-1)

or 
String.fromCharCode(e.which) == ',' && customPrices.charAt(customPrices.length-1) == ','