Validation 验证包含两组电话号码的文本框

Validation 验证包含两组电话号码的文本框,validation,uwp,phone-number,Validation,Uwp,Phone Number,我正在尝试对一个文本框进行验证,该文本框允许在单个文本框中输入一个或多个电话号码。我要做的是向文本框中包含的电话号码发送消息。 当我只在文本框中输入一组数字,信息就可以发送时,我没有问题。 但是,每当我在同一文本框中键入两组数字时,就会出现验证错误 我正在使用用户控件并将用户控件放在listview中 这是我的密码: 以下是按钮点击事件的代码: 我试着用一个字母把这两个数字分开;签名我想知道这是否是问题所在。或者可能是我放置的匹配模式。答案很简单,在TextFieldInputControlVi

我正在尝试对一个文本框进行验证,该文本框允许在单个文本框中输入一个或多个电话号码。我要做的是向文本框中包含的电话号码发送消息。 当我只在文本框中输入一组数字,信息就可以发送时,我没有问题。 但是,每当我在同一文本框中键入两组数字时,就会出现验证错误

我正在使用用户控件并将用户控件放在listview中

这是我的密码:

以下是按钮点击事件的代码:


我试着用一个字母把这两个数字分开;签名我想知道这是否是问题所在。或者可能是我放置的匹配模式。

答案很简单,在TextFieldInputControlViewModel中创建一个bool属性

public bool AcceptMultiple {get;set;}
要保持动态,请创建一个char属性作为分隔符,如下所示:

public char Separator {get;set;}
new TextFieldInputControlViewModel(){Separator = ';', AcceptMultiple = true, ColumnWidth = new GridLength(350) ,HeaderName = "Recipient's mobile number *"  , IsMandatory = true, MatchingPattern = @"^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$", Tag="phone", ContentHeight = 45, ErrorMessage = "Please enter recipient mobile number. "},
if(AcceptMultiple)
{
    if(Separator == null)
        throw new ArgumentNullException("You have to provide a separator to accept multiple entries.");

    string[] textItems = textField.Split(Separator);
    if(textItems?.Length < 1)
    {
        ErrorMessage = "Please enter recipient mobile number." //assuming that this is your field for what message has to be shown.
        IsError = true; //assuming this is your bool field that shows all the errors
        return;
    }

    //do a quick check if the pattern matching is mandatory. if it's not, just return.
    if(!IsMandatory)
        return;

    //your Matching Regex Pattern
    Regex rgx = new Regex(MatchingPattern);

    //loop through every item in the array to find the first entry that's invalid
    foreach(var item in textItems)
    {
        //only check for an invalid input as the valid one's won't trigger any thing.
        if(!rgx.IsMatch(item))
        {
            ErrorMessage = $"{item} is an invalid input";
            IsError = true;
            break;  //this statement will prevent the loop from continuing.
        }
    }
}
现在,通过向新字段添加值修改新的TextFieldInputControlViewModel代码语句,如下所示:

public char Separator {get;set;}
new TextFieldInputControlViewModel(){Separator = ';', AcceptMultiple = true, ColumnWidth = new GridLength(350) ,HeaderName = "Recipient's mobile number *"  , IsMandatory = true, MatchingPattern = @"^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$", Tag="phone", ContentHeight = 45, ErrorMessage = "Please enter recipient mobile number. "},
if(AcceptMultiple)
{
    if(Separator == null)
        throw new ArgumentNullException("You have to provide a separator to accept multiple entries.");

    string[] textItems = textField.Split(Separator);
    if(textItems?.Length < 1)
    {
        ErrorMessage = "Please enter recipient mobile number." //assuming that this is your field for what message has to be shown.
        IsError = true; //assuming this is your bool field that shows all the errors
        return;
    }

    //do a quick check if the pattern matching is mandatory. if it's not, just return.
    if(!IsMandatory)
        return;

    //your Matching Regex Pattern
    Regex rgx = new Regex(MatchingPattern);

    //loop through every item in the array to find the first entry that's invalid
    foreach(var item in textItems)
    {
        //only check for an invalid input as the valid one's won't trigger any thing.
        if(!rgx.IsMatch(item))
        {
            ErrorMessage = $"{item} is an invalid input";
            IsError = true;
            break;  //this statement will prevent the loop from continuing.
        }
    }
}
完成后,现在在checkValidation函数中或检查验证或模式匹配的位置可以替换为以下内容:

public char Separator {get;set;}
new TextFieldInputControlViewModel(){Separator = ';', AcceptMultiple = true, ColumnWidth = new GridLength(350) ,HeaderName = "Recipient's mobile number *"  , IsMandatory = true, MatchingPattern = @"^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$", Tag="phone", ContentHeight = 45, ErrorMessage = "Please enter recipient mobile number. "},
if(AcceptMultiple)
{
    if(Separator == null)
        throw new ArgumentNullException("You have to provide a separator to accept multiple entries.");

    string[] textItems = textField.Split(Separator);
    if(textItems?.Length < 1)
    {
        ErrorMessage = "Please enter recipient mobile number." //assuming that this is your field for what message has to be shown.
        IsError = true; //assuming this is your bool field that shows all the errors
        return;
    }

    //do a quick check if the pattern matching is mandatory. if it's not, just return.
    if(!IsMandatory)
        return;

    //your Matching Regex Pattern
    Regex rgx = new Regex(MatchingPattern);

    //loop through every item in the array to find the first entry that's invalid
    foreach(var item in textItems)
    {
        //only check for an invalid input as the valid one's won't trigger any thing.
        if(!rgx.IsMatch(item))
        {
            ErrorMessage = $"{item} is an invalid input";
            IsError = true;
            break;  //this statement will prevent the loop from continuing.
        }
    }
}
这样就行了

我假设了几个变量名,因为问题中缺少信息。我在评论中提到了这一点。一定要更换它们


我是在xaml.cs下还是在视图模型下添加代码。。。我想允许输入一组或多组数字。。。因此,我必须更改上面给出的if语句吗?新的TextFieldInputControlViewModel进入xaml.cs,而方法进入TextFieldInputControlViewModel类