I want to check for specific field is valid in my .NET Core MVC Project. I know there is the ModelState.IsValid method which check the entire Model, but i need to check only one property in the model.
below is my Model:
public class MyModel
{
[Required(AllowEmptyStrings = false)]
[StringLength(10)]
[RegularExpression(@"^[0-9]*$", ErrorMessage = "Invalid ID")]
[Display(Name = "IdentityNo")]
[Required(AllowEmptyStrings = false)]
[StringLength(256)]
[RegularExpression(@"^[A-Za-z0-9\s!@#$%^&*()_+=-`~\\\]\[{}|';:/.,?]*$", ErrorMessage ="Invalid Email or username")]
public string UserNameOrEmailAddress { get; set; }
[Required(AllowEmptyStrings = false)]
[StringLength(50)]
[DataType(DataType.Password)]
public string Password { get; set; }
}
If i use ModelState.IsValid will check all the model, and i want to check only ID property. below is how i check the validation in the Controller:
if (ModelState.IsValid)
{
// code
}
it's will return false, because i only entry ID, username and password leve it empty.
How to check specific property in my Model?