title: .NET regex specific to .net C# dataannotations
body: <p>I have a regular expression that i'm trying to figure out for a .NET Data Annotation Validation model that i'm using.  Currently it looks like this</p>
  
  <pre><code>[RegularExpression("^((?!City)[a-zA-Z '])+$", ErrorMessage = "City is required and must be properly formatted.")]
  </code></pre>
  
  <p>The first half of this regex is to account for City, the second half is to make sure its only letters and spaces.  </p>
  
  <p>The default value for the input text box is City, so just "City" cant pass validation.  I can't seem to get the first section of that regex to have these cases pass.</p>
  
  <pre><code>Cityville  
  City City
  Jersey City
  Arizona City
  </code></pre>
  
  <p>But this case not pass:</p>
  
  <pre><code>City
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>*<strong><em>EDIT</em>*</strong>
  This one did the trick:</p>
  
  <p>^((?!^City$)[a-zA-Z '])+$</p>
  
  <p>I updated all of my validation to include this.  Just in case you want to see what i'm doing.  Here if the code for the whole validation data model that i'm using. (updated with the correct regex)</p>
  
  <pre><code>public class FormModel {
      [Required(ErrorMessage = "First Name is a Required field.")]
      [DataType(DataType.Text)]
      [Display(Order = 1, Name = "FirstName")]
      [RegularExpression("^((?!^First Name$)[a-zA-Z '])+$", ErrorMessage = "First name is required and must be properly formatted.")]
      public string FirstName { get; set; }
  
      [Required(ErrorMessage = "Last Name is a Required field.")]
      [DataType(DataType.Text)]
      [Display(Order = 2, Name = "LastName")]
      [RegularExpression("^((?!^Last Name$)[a-zA-Z '])+$", ErrorMessage = "Last name is required and must be properly formatted.")]
      public string LastName { get; set; }
  
      [Required(ErrorMessage = "Address is a Required field.")]
      [DataType(DataType.Text)]
      [Display(Order = 3, Name = "Address")]
      [RegularExpression("^((?!^Address$)[0-9A-Za-z #.,])+$", ErrorMessage = "Address is required and must be properly formatted.")]
      public string Address { get; set; }
  
      [DataType(DataType.Text)]
      [Display(Order = 4, Name = "Address2")]
      public string Address2 { get; set; }
  
      [Required(ErrorMessage = "City is a Required field.")]
      [DataType(DataType.Text)]
      [RegularExpression("^((?!^City$)[a-zA-Z '])+$", ErrorMessage = "City is required and must be properly formatted.")]
      [Display(Order = 6, Name = "City")]
      public string City { get; set; }
  
      [Required( ErrorMessage = "State is a Required field." )]
  [DataType( DataType.Text )]
  [Display( Order = 7, Name = "State" )]
  public IEnumerable&lt;String&gt; State { get; set; }
  
  [Required( ErrorMessage = "Zip is a Required field." )]
  [DataType( DataType.Text )]
  [RegularExpression( "\\d{5}",
          ErrorMessage = "Zip Code is required and must be properly formatted.")]
  [Display( Order = 8, Name = "Zip" )]
  public String Zip { get; set; }
  
  [Required( ErrorMessage = "Phone is a Required field." )]
  [DataType( DataType.PhoneNumber )]
  [RegularExpression( "^[01]?[- .]?\\(?[2-9]\\d{2}\\)?[- .]?\\d{3}[- .]?\\d{4}$",
          ErrorMessage = "Phone is required and must be properly formatted.")]
  [Display( Order = 9, Name = "Phone" )]
  public string Phone { get; set; }
  
  [Required( ErrorMessage = "Email is a Required field." )]
  [DataType( DataType.EmailAddress )]
  
  [RegularExpression( "^[A-Za-z0-9._%+-]*@[A-Za-z0-9.-]*\\.[A-Za-z0-9-]{2,}$",
          ErrorMessage = "Email is required and must be properly formatted.")]
  [Display( Order = 10, Name = "Email" )]
  public string Email { get; set; }
  </code></pre>