Currently developing an application for Windows Phone that makes use of the bing maps geocode service. I have a search box where users can type in a location and then this is sent to bing maps which returns a geocoordinate.
I have however, run into an issue with the search box. When I enter random letters no results are found and I have implemented code so the user is informed that there are no destinations found. The problem is though that if for whatever the reason the user decides to write some symbols eg: &$%£” this crashes the application when bing maps returns some data.
How would I go about validating the textbox entry so that it checks to see if there are any symbols in the string and then sends this off to the service if there are not?
Any help is much appreciated!
The best option is to prevent the user from entering invalid characters. This can be done by using Regular Expressions within the TextChanged event. You can either go with a permissive or restrictive model.
Be sure to add the necessary using statement:
In the permissive model, you allow all characters except for ones you have specifically forbidden:
In the restrictive model, you only allow specific characters:
Generally the restrictive model will be easier to maintain (you won’t have to go back and keep adding additional forbidden symbols as you discover them), but it really depends on your application. In either case, any uses of forbidden symbols will be ignored completely as if the user didn’t hit that key.
As a side note, your
textboxshould have it’sInputScopeset toMaps.