How to use DataAnnotations & Regex - c# - ASP.NET Core

Viewed 1982

I am using dataannotations to match fields a user will input for error handling purposes.

Whenever they input an incorrect format, it prints the actual regex and I was wondering if there was a way to remove the "match the following regex" and just print the error message.

Example of my form and the error message displayed: Form

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace FoodCart.Models
{
    public class Order
    {
        public int ID { get; set; }

        [Display(Name = "First Name")]
        [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
        [Required(ErrorMessage = "This field is required")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
        [Required(ErrorMessage = "This field is required")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public string Address { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public string City { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public string State { get; set; }

        [Required(ErrorMessage = "This field is required")]
        public string Zip { get; set; }

        [Display(Name = "Name on Card")]
        [Required(ErrorMessage = "This field is required")]
        public string NameonCard { get; set; }

        [Display(Name = "Credit Card Number")]
        [Required(ErrorMessage = "This field is required, input valid 16 digit number")]
        [RegularExpression(@" ([0 - 9][0 - 9][0 - 9][0 - 9]) ([0 - 9][0 - 9][0 - 9][0 - 9]) ([0 - 9][0 - 9][0 - 9][0 - 9]) ([0 - 9][0 - 9][0 - 9][0 - 9])")]
        public string CreditCardNum { get; set; }

        [Display(Name = "Expiration Date")]
        [Required(ErrorMessage = "This field is required, input valid expiration date: MM/YY")]
        [RegularExpression(@"([0-9][0-9])\/([0-9][0-9])")]
        public string ExpDate{ get; set; }

        [Display(Name = "CVV")]
        [RegularExpression(@"([0-9][0-9][0-9])")]
        [Required(ErrorMessage = "This field is required")]
        public string cvv { get; set; }

        [Display(Name = "Total")]
        public decimal YourTotal { get; set; }
        public int UserLinkID { get; set; }


    }
}
2 Answers

The validation attributes all come with a default error message that is built into the implementation. For the RegularExpressionAttribute, this error message is the following:

The field {0} must match the regular expression '{1}'.

The placeholder {0} will be the display name of the property that is being validated and {1} will be the regular expression itself.

There are a few ways to customize this error message, using one of the properties on the validation attribute. The simplest way would be to specify the error message directly with the ErrorMessage property. Note that you can still refer to the placeholders {0} (for the field name) and {1} (for the regular expression) if you like:

[Display(Name = "First Name")]
[RegularExpression(@"^[a-zA-Z'-\s]{1,40}$", ErrorMessage = "The field {0} is not a valid name")]
public string FirstName { get; set; }
[RegularExpression(@"([0-9][0-9])\/([0-9][0-9])",ErrorMessage = "your message")]

Related