Validating input forms in Windows Phone 7 - by Pete Vickers
Validating text box fields in any application has always been a pain, and I have always tried to use short-cuts to validate input. Windows Phone 7 is no exception, so hopefully this will help you ease through field validation.
My ideal method has always been to loop through the controls, and validate any text boxes I come across. To help me do this, I have always used the 'tag' property of the text box to determine what needs doing. I use a list of items in the tag, to determine what I need to validate. So, for a required numeric field for an 'order qty' for example, I would set the tag to "order qty,numeric". If I had a text box that must be entered, be numeric, and have a minimum value of 1 and a maximum value of 1000, then my tag would be "order qty,numeric,1,1000". This can be extended in any way you want, to check for numeric, alphabetic etc. etc. I have a routine called "valid_numeric_fields" which will test a passed string for numeric, min and max values and whether decimal places or negatives are acceptable.
The next step would be to loop through the controls in the form, or the panel or some container control. Prior to Windows Phone 7, I would just iterate through the controls collection, but as I soon found out, the rules changed in Windows Phone 7. Not to panic though, it is still possible. In this sample, my text boxes are all contained in a "stackpanel", and all have textblocks for labels. The sample can be extended to use other controls, but here we will concentrate on text boxes. When the user clicks on the control to say the have filled in the form, we call a routine called "validatesok" which returns a Boolean.
Read More from: Original Source
Post Contributed by: Kunal Chowdhury
Kunal is the Site Admin and Contributor of Silverlight-Zone. He is a Software Engineer, Microsoft Silverlight MVP, Code Project Mentor and a Code Project MVP. He is also an active Author in SilverlightShow.net and a speaker in various community events. He works on Microsoft Platform and very passionate about Silverlight technology. He started his career in 2007 and achieved various awards during his professional life.
He shares his findings in his personal blog: http://www.kunal-chowdhury.com and he also tweets at: @kunal2383.