Created
April 10, 2012 13:27
-
-
Save jchannon/2351348 to your computer and use it in GitHub Desktop.
Comparing 2 model properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [AttributeUsage(AttributeTargets.Class)] | |
| public class MatchAttribute : ValidationAttribute | |
| { | |
| public String SourceProperty { get; set; } | |
| public String MatchProperty { get; set; } | |
| public MatchAttribute(string source, string match) | |
| { | |
| SourceProperty = source; | |
| MatchProperty = match; | |
| } | |
| public override Boolean IsValid(Object value) | |
| { | |
| if (value == null) | |
| return false; | |
| Type objectType = value.GetType(); | |
| PropertyInfo[] properties = objectType.GetProperties(); | |
| object sourceValue = new object(); | |
| object matchValue = new object(); | |
| Type sourceType = null; | |
| Type matchType = null; | |
| int counter = 0; | |
| foreach (PropertyInfo propertyInfo in properties) | |
| { | |
| if (propertyInfo.Name == SourceProperty || propertyInfo.Name == MatchProperty) | |
| { | |
| if (counter == 0) | |
| { | |
| sourceValue = propertyInfo.GetValue(value, null); | |
| sourceType = propertyInfo.GetValue(value, null).GetType(); | |
| } | |
| if (counter == 1) | |
| { | |
| matchValue = propertyInfo.GetValue(value, null); | |
| matchType = propertyInfo.GetValue(value, null).GetType(); | |
| } | |
| counter++; | |
| if (counter == 2) | |
| { | |
| break; | |
| } | |
| } | |
| } | |
| if (sourceType != null && matchType != null) | |
| { | |
| //if (Convert.ChangeType(sourceValue, sourceType) == Convert.ChangeType(matchValue, matchType)) | |
| return String.Equals(sourceValue.ToString(), matchValue.ToString()); | |
| } | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using System.ComponentModel.DataAnnotations; | |
| using DinnerParty.Models.CustomAnnotations; | |
| namespace DinnerParty.Models | |
| { | |
| [Match("Password", "ConfirmPassword", ErrorMessage = "Passwords must match")] | |
| public class RegisterModel | |
| { | |
| [Required] | |
| public string UserName { get; set; } | |
| [Required] | |
| [RegularExpression(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$", ErrorMessage = "Please enter a valid email address")] | |
| public string Email { get; set; } | |
| [StringLength(18, MinimumLength = 8, ErrorMessage = "Password lengths must be between 8 and 18 characters in length"), Required] | |
| public string Password { get; set; } | |
| [Required] | |
| public string ConfirmPassword { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment