Skip to content

Basic Validator usage (non generic)

Jon edited this page Apr 24, 2019 · 2 revisions

Simple validation

The simplest way to validate is to call each validation method providing all the data required.

validator.IsEmailAddress("myemail.com", "email").Validate();

The example above validates the email address myemail.com, for the property name of email and will use the default error message. Lets break this down a little:

  • The fist property is what we want to validate, i.e. a value that has been passed back from the UI.
  • The second value is the name of the property you wish to validate. For example in the model or UI it could be called, email, password, confirmPassword. If there is an error it will be assigned to the value of this property.
  • the last and optional parameter is an error message (string format). If it is left blank the default will be used. It uses a string.Format taking each of the parameters (excluding the first value, email in this case). For example MaxLength has: public static string MaxLength => "{1} should not be longer than {0}"; where {1} is propName and {0} is maxLength.