//.net data Validation, an open source project, direct download
1 usingfluentvalidation;2 3 Public classCustomervalidator:abstractvalidator<customer> {4 PublicCustomervalidator () {5Rulefor (Customer =customer. Surname). Notempty ();6Rulefor (customer = customer. forename). Notempty (). Withmessage ("Please specify a first name");7Rulefor (customer = customer. Discount). NotEqual (0). When (customer =customer. Hasdiscount);8Rulefor (customer = customer. Address). Length ( -, -);9Rulefor (customer = customer. Postcode). Must (Beavalidpostcode). Withmessage ("Please specify a valid postcode");Ten } One A Private BOOLBeavalidpostcode (stringpostcode) { - //Custom postcode validating logic goes here - } the } - -Customer customer =NewCustomer (); -Customervalidator Validator =Newcustomervalidator (); +Validationresult results =Validator. Validate (customer); - + BOOLvalidationsucceeded =results. IsValid; Ailist<validationfailure> failures = results. Errors;
Fluent Validation for. NET