Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Wanted to remove regular express validation only append required validation

i am not able remove regular express validation

C#
public class HomeController : Controller
   {

       public ActionResult Index()
       {
           ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
           return View();
       }

       [HttpPost]
       public ActionResult Index(Person objPerson)
       {

           if (ModelState.IsValid)
           {

           }
           return View(objPerson);
       }

   }



   public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
   {
       protected override IEnumerable<modelvalidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<attribute> attributes)
       {
           if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "FirstName")
           {
                attributes = new List<attribute>() { new RequiredAttribute() };
           }
           return base.GetValidators(metadata, context, attributes);
       }
   }


   public class Person
   {
       [RegularExpression(@"^[a-zA-Z0-9\-\s]*$")]
       public string FirstName { get; set; }
   }


just i want to remove regular expression validatin check only need to add required validation

What I have tried:

DataAnnotationsModelValidatorProvider got way to add dynamic validation on run time
Posted
Updated 19-Oct-16 3:56am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900