Xamarin: Generic Input Validator Class

One of my favorite coding projects is to write a very generic all purpose utility classes. When I was picking up Xamarin last year, I decided to create some for myself and the team. Today, I’m going to share one of my utility classes – the validator class. You may download the source from GitHub here.


Some key features of Validator class.

  • Validator can be applied in all input form – any platform in Xamarin (including input generated by Xamarin.Forms)
  • You can use this class as a shared project, then add as reference into your respective platform library.
  • You may edit it to suit your own project codes – source is in git.


I’ve prepared a sample (Native) cross platform app project on how a typical sign up form will be. Sample project is to show you how this class is implemented.

Sample pic shows the view in platform Xamarin.Droid and Xamarin.iOS; each having their own view with similar input fields. Below are the fields we set for let’s say a sign up scenario.

Android axml
IOS storyboard view
  1. Full Name – 200 string characters max length. (*mandatory field)
  2. Alias – 30 string characters max length (optional)
  3. Email – 256 characters max length (*mandatory & email validation needed)
  4. Age – (*mandatory & integer only allowed)
  5. Mobile number – (*mandatory & mobile like integer only allowed)
  6. Employee Code – (*mandatory & required pattern matching of first character is an alphabet followed by 4 digit number e.g. A1947)

Upon tapping Submit button, we will perform a simultaneous validation check on all inputs and will inform the user, by changing the placeholder text and color of the input text background to pink. This is to clearly identify the fields required for attention.

To simplify matter, I will insert the utility class into the shared project folder under ValidationForm -> Com -> IsGoodStuff -> Validator.

The validator works best when the we assigned all our input (UITextField / EditText) into a form of array. Aside from that, we also need to instruct the validator on what criteria to check against.

For example, the field Full Name has a validation criteria of NOTNULL. This means this fields is mandatory and must be filled up to pass the validation.

Validator comes with some preset static values to ease the criteria input. These criteria are as follows (refer to source at MainActivity.cs & ViewController.cs) :-

  • Criteria.NONE – just ignore any validation checking.
  • Criteria.NOTNULL – returns error if the values is empty.
  • Criteria.EMAIL – returns error if input value is not a valid email e.g. abc@gmail.com
  • Criteria.INTEGERONLY – returns error if alphabet or special characters is present. Only pure numerical is allowed
  • Criteria.FLOATONLY – return error if the parsing of float fails
  • Criteria.MOBILE_NUMBER – right now this is based on pattern checking of my country. You modify this to suit your country code mobile.
  • Criteria.MINIMUMCHAR6 – normally used for password, whereby they required more than 6 characters values.
  • {Pattern Wildcard} – if none of the above is entered, validator will assume the Criteria itself is a pattern string used for RegEx. (See sample code for Field 6 – Employee Code)

For this sample, to store the input object along side with it’s Criteria, I’ve introduced a Class called FormObject. 

       public class FormObject
		// in IOS 
		public UITextField FormObj { set; get; }
		// in Android 
		public EditText FormObj { set; get; }
		public string Criteria { set; get; }

FormObject contains the property of UITextField/ EditText and Criteria, a string based property to store the validation criteria. FormObject is then, declared as an array of 6 items.

Next, we iterate the array of FormObject to assign the values.

Validator will start validating each FormObj based on the Criteria input, when submit button is tapped. ErrorCount is used to keep track of errors. If the count is 0 simply means all green and good to go!. Code below shows, the logic when the button submit is tapped. (* NOTE: code may looked different in terms of UI component names and properties)

                int resultCount = 0;
		var ValidateEngine = new Validator();
		for (var i = 0; i < MyTextUI.Length; i++)
			if (!ValidateEngine.CheckFor(MyTextUI[i].FormObj.Text, (string)MyTextUI[i].Criteria))
				// Note: If CheckFor returns false means there's an error.
				// You can also take this opportunity to highlight the error field.
				// or use HintText to display the cause of error etc.
				MyTextUI[i].FormObj.BackgroundColor = UIColor.FromRGB(255, 192, 203);
				MyTextUI[i].FormObj.Text = "";
		if (resultCount != 0)
			// Show Error Message
		} else {
			// Show Ok Message

ResetFieldErrorState is basically a method to reset back all the highlighted error marked fields when the submit button is hit again and you are good to go. You may also enhance the user experience by resetting the individual fields when user focussed their cursor and limit the maximum character input or type of keyboard etc. Well thats about it folks – See you in next post!



Leave a Reply

Your email address will not be published. Required fields are marked *