Skip to content

Instantly share code, notes, and snippets.

@chrisdiana
Last active August 29, 2015 14:06
Show Gist options
  • Save chrisdiana/39b8a7da06c1da5ce14f to your computer and use it in GitHub Desktop.
Save chrisdiana/39b8a7da06c1da5ce14f to your computer and use it in GitHub Desktop.
Text Field Validation for UITextField in Xamarin
// sample text field
var titleField = new UITextField (new RectangleF (0, 160, UIScreen.MainScreen.Bounds.Width, 50)){
BackgroundColor = UIColor.White,
TextColor = ViewHelpers.DarkGray,
Placeholder = "Title:"
};
// validation
titleField.EditingDidEnd += (object sender, EventArgs e) => {
if ( ((UITextField)sender).Text.Length <= 0 ) {
InvokeOnMainThread ( () => {
titleField.Layer.BorderColor = ViewHelpers.DarkBlue.CGColor;
titleField.Layer.BorderWidth = 2;
titleField.Text = "Please fill in the Title:";
} );
}else if ( ((UITextField)sender).Text.Length > 0 ){
titleField.Layer.BorderColor = ViewHelpers.DarkBlue.CGColor;
titleField.Layer.BorderWidth = 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment