简体   繁体   English

将文本输入限制为以非连续字符分隔的数字组

[英]Restrict text input to number groups separate by a non-consecutive character

I've been doing a lot of searching, chopping and changing, but I'm...slightly lost, especially with regards to many of the regex examples I've been seeing. 我一直在进行大量的搜索,修改和更改,但是我...迷路了,尤其是在我所见过的许多正则表达式示例方面。

This is what I want to do: 这就是我想做的:

I have a text input field, size 32. 我有一个文本输入字段,大小为32。

I want users to enter their telephone numbers in it, but I want them to enter a minimum of 10 numbers, separated by a single comma. 我希望用户在其中输入他们的电话号码,但我希望他们输入至少10个数字,并用一个逗号分隔。 Example: 例:

Eg 1 0123456789,0123456789 = right (first group is >=10 numbers, second group = >=10 numbers & groups are separated by a single comma, no spaces or other symbols) 例如1 0123456789,0123456789 =右(第一个组> = 10个数字,第二个组=> = 10个数字,并且组用单个逗号分隔,没有空格或其他符号)

Eg 2 0123456789,,0123456789 = wrong (because there are 2 commas) 例如2 0123456789,,0123456789 =错误(因为有2个逗号)

Eg 3 0123456789,0123456789,0123456789 = right (same concept as Eg 1, but with 3 groups) 例如3 0123456789,0123456789,0123456789 =正确(与例如1相同的概念,但有3个组)

I've got the following, but it does not limit the comma to 1 per 10 numbers, and it does not impose a minimum character count on the number group. 我有以下内容,但它没有将逗号限制为每10个数字1个,并且没有对数字组施加最小字符数。

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

//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && String.fromCharCode(e.which) != ',' 
&& (e.which < 48 || e.which > 57)) {

//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});

Preferably, I'd like to warn the user of where they are going wrong as well. 最好是,我也想警告用户他们出了错。 For example, if they try to enter two commas, I'd like to specifically point that out in the error, or if they havent inserted enough numbers, i'd like to specifically point that out in the error. 例如,如果他们尝试输入两个逗号,我想在错误中特别指出,或者如果他们没有插入足够的数字,我想在错误中特别指出。 I'd also like to point out in the error when neither a number or a comma is inserted. 我也想指出当没有插入数字或逗号时的错误。 I'd like to ensure that the tab, and F5 keys are not disabled on the keyboard as well. 我想确保也不要在键盘上禁用选项卡和F5键。 And very importantly, I'd like to specifically detect when the plus or addition key is used, and give a different error there. 而且非常重要的是,我想专门检测何时使用加号或加号键,并在那里给出一个不同的错误。 I think I'm asking for something a little complex and uninviting so sorry :/ 我想我要求的是一些复杂且令人不快的东西,很抱歉:/

The example code I provided above works pretty well across all browsers, but it doesn't have any of the minimum or maximum limits on anything I've alluded to above. 我上面提供的示例代码在所有浏览器上都可以很好地工作,但是对我上面提到的任何内容都没有任何最小或最大限制。

Any help would be appreciated. 任何帮助,将不胜感激。

至于将检查输入是否有效的正则表达式(1-3个电话号码,正好为10位数字,用单个逗号分隔),您可以执行以下操作:

^\d{10}(,\d{10}){0,2}$

Try like the below snippet without Regex 尝试使用以下不含正则Regex代码段

var errrorMessage = '';

function validateLength (no) {
    if(!no.length == 10) {
       return false;
    }
    return true;
}

function validatePhoneNumbers (currentString, splitBy) {
    if(currentString) {
        var isValid = true,
            currentList = currentString.split(splitBy);

        // If there is only one email / some other separated strings, Trim and Return.
        if(currentList.length == 1) {
            errrorMessage = 'Invalid Length in Item: 1';
            if(validateLength( currentString.trim() )) isValid = false;
        }
        else if(currentList.length > 1) {
            // Iterating mainly to trim and validate.
            for (var i = 0; i < currentList.length; i++) {
                var listItem = currentList[i].trim();
                if( validateLength(listItem ) ) {
                   isValid = false;
                   errrorMessage = 'Invalid Length in Item:' + i
                   break;
                }
                // else if for some other validation.
            }
        }
    }

    return isValid;
}

validatePhoneNumbers( $("#lastname").val() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM