Jam*_*ndy 2 validation silverlight textbox bing-maps windows-phone-7
目前正在开发一个使用bing map地理编码服务的Windows Phone应用程序.我有一个搜索框,用户可以在其中键入位置,然后将其发送到返回地理坐标的bing贴图.
但是,我遇到了搜索框的问题.当我输入随机字母时,没有找到结果,我已经实现了代码,因此用户被告知没有找到目的地.问题是,如果用户决定写一些符号的原因,例如:&$%£"这会在bing map返回一些数据时崩溃应用程序.
我将如何验证文本框条目,以便检查字符串中是否有任何符号,如果没有,则将其发送给服务?
任何帮助深表感谢!
最好的选择是防止用户输入无效字符.这可以通过在TextChanged事件中使用正则表达式来完成.您可以使用许可或限制模型.
请务必添加必要的using语句:
using System.Text.RegularExpressions;
Run Code Online (Sandbox Code Playgroud)
在许可模型中,您允许除了您明确禁止的所有字符之外的所有字符:
private Regex rxForbidden = new Regex(@"[&$%£]", RegexOptions.IgnoreCase);
private void txtInput_TextChanged(object sender, TextChangedEventArgs e)
{
txtInput.Text = rxForbidden.Replace(txtInput.Text, "");
txtInput.SelectionStart = txtInput.Text.Length;
}
Run Code Online (Sandbox Code Playgroud)
在限制性模型中,您只允许特定字符:
private Regex rxForbidden = new Regex(@"[^0-9a-z]", RegexOptions.IgnoreCase);
private void txtInput_TextChanged(object sender, TextChangedEventArgs e)
{
txtInput.Text = rxForbidden.Replace(txtInput.Text, "");
txtInput.SelectionStart = txtInput.Text.Length;
}
Run Code Online (Sandbox Code Playgroud)
通常,限制性模型将更容易维护(您不必返回并在发现时继续添加其他禁用符号),但这实际上取决于您的应用程序.在任何一种情况下,禁止符号的任何使用都将被完全忽略,就像用户没有按下该键一样.
作为旁注,你textbox应该把它InputScope设置为Maps.
| 归档时间: |
|
| 查看次数: |
1943 次 |
| 最近记录: |