Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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