using monotouch how can I validate user entered data? I am creating a simply contact form for my iPhone app.
In asp.net I would maybe use a validation summary to display validation messages and required fields or check email address input against a regex.
However, for the iPhone I figured that when the user has entered some data into a UITextField I could validate that user on a touch Event and then display a validation message in a Alert dialog?
How can I make my window form validate in this way?
I think this is the way you do it:
Subclass UITextFieldDelegate. Override ShouldEndEditing(). Validate the user entered data in ShouldEndEditing(). If the data is invalid, create and show a UIAlertView explaining why the data is invalid and return false from ShouldEndEditing() (or use some other means to signal the existence of a problem). If the data is valid, return true. Assign a UITextFieldDelegate subclass object to the UITextField object's Delegate property.
Paraphrasing the Apple documentation:
This method
[ShouldEndEditing()]is called when the text field is asked to resign the first responder status. This might occur when your application asks the text field to resign focus or when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text field calls this method to give your delegate a chance to decide whether it should.Normally, you would return "true" from this method to allow the text field to resign the first responder status. You might return "false," however, in cases where your delegate detects invalid contents in the text field. By returning "false," you could prevent the user from switching to another control until the text field contained a valid value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With