带有两组电话号码的文本框的验证

编程入门 行业动态 更新时间:2024-10-17 09:53:07
本文介绍了带有两组电话号码的文本框的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to do a validation on a textbox that can allow the input of one or more phone number in a single textbox. What I am trying to do is to send an message to the phone numbers included in the textbox. I have no problem when I enter just one set of number into the textbox and the message can be sent. However, whenever I type two sets of digit into the same textbox, my validation error will appear.

我正在使用用户控件,并将用户控件置于列表视图中.

I am using user controls and putting the user control in a listview.

这是我的代码:

private ObservableCollection<IFormControl> formFields; internal ObservableCollection<IFormControl> FormFields { get { if (formFields == null) { formFields = new ObservableCollection<IFormControl>(new List<IFormControl>() { new TextFieldInputControlViewModel(){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. "}, }); } return formFields; } }

这是按钮单击事件的代码:

private void OkButton_Click(object sender, RoutedEventArgs e) { MessageDialog clickMessage; UICommand YesBtn; int result = 0; //Fetch Phone number var phoneno = FormFields.FirstOrDefault(x => x.Tag?.ToLower() == "phone").ContentToStore; string s = phoneno; string[] numbers = s.Split(';'); foreach (string number in numbers) { int parsedValue; if (int.TryParse(number, out parsedValue) && number.Length.Equals(8)) { result++; } else { } } if (result.Equals(numbers.Count())) { try { for (int i = 0; i < numbers.Count(); i++) { Class.SMS sms = new Class.SMS(); sms.sendSMS(numbers[i], @"Hi, this is a message from Nanyang Polytechnic School of IT. The meeting venue is located at Block L." + Environment.NewLine + "Click below to view the map " + Environment.NewLine + location); clickMessage = new MessageDialog("The SMS has been sent to the recipient."); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += timer_Tick; timer.Start(); YesBtn = new UICommand("Ok", delegate (IUICommand command) { timer.Stop(); idleTimer.Stop(); var rootFrame = (Window.Current.Content as Frame); rootFrame.Navigate(typeof(HomePage)); rootFrame.BackStack.Clear(); }); clickMessage.Commands.Add(YesBtn); clickMessage.ShowAsync(); } } catch (Exception ex) { } } }

我正在尝试用;"分隔两个数字;" ....,我想知道是否是问题所在.或者也许是我放入的匹配模式.

I am trying to separate the two numbers with ";" sign.... and I am wondering if that is the problem. Or maybe it is the matchingpattern that I have placed in.

推荐答案

答案很简单,在

public bool AcceptMultiple {get;set;}

为了保持动态,创建一个char属性作为分隔符,如下所示:

and to keep things dynamic, create a char property as a separator like below:

public char Separator {get;set;}

现在,通过将值添加到新字段中来修改new TextFieldInputControlViewModel()代码语句,如下所示:

Now, modify your new TextFieldInputControlViewModel() code statement by adding values to your new fields like below:

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. "},

完成后,现在可以在checkValidation()函数(或检查验证或模式匹配的位置)中将其替换为以下内容:

Once it's done, now in your checkValidation() function (or where you check the validation or pattern match) can be replaced with something like below:

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. } } }

然后就可以了.

我以一些变量名作为假设,因为问题中缺少该信息.我在关于它们的评论中提到了它.确保更换它们.

I've taken a few variable names as an assumption as the information was missing in the question. I've mentioned it in the comments about them. Make sure you replace them.

更多推荐

带有两组电话号码的文本框的验证

本文发布于:2023-11-24 23:21:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两组   文本框   电话号码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!