I want to validate a date of birth with a RemoteValidation/jQuery from 3 selectors. I want to check if a user is 18 or older. But atm i'm having some trouble to group them so they are invalid/valid together and with only one errormessage.
Goal: i want to validate all 3 elements as a group (jquery validator group) with the same RemoteValidation when you change anyone of them.
Anyone got any idea how to solve this?
View:
@Html.DropDownListFor(m => m.BirthYear, Model.BirthYearList)
@Html.DropDownListFor(m => m.BirthMonth, Model.BirthMonthList)
@Html.DropDownListFor(m => m.BirthDay, Model.BirthDayList)
Model
[Remote("IsValidCustomerBirthDate", "JsonValidation", AdditionalFields = "BirthDay, BirthMonth", ErrorMessageResourceName = "InvalidCustomerBirthDate", ErrorMessageResourceType = typeof(Error))]
[Required]
public int BirthYear { get; set; }
[Remote("IsValidCustomerBirthDate", "JsonValidation", AdditionalFields = "BirthYear, BirthDay", ErrorMessageResourceName = "InvalidCustomerBirthDate", ErrorMessageResourceType = typeof(Error))]
[Required]
public int BirthMonth { get; set; }
[Remote("IsValidCustomerBirthDate", "JsonValidation", AdditionalFields = "BirthYear, BirthMonth", ErrorMessageResourceName = "InvalidCustomerBirthDate", ErrorMessageResourceType = typeof(Error))]
[Required]
public int BirthDay { get; set; }
RemoteValidation function
[HttpGet]
public JsonResult IsValidCustomerBirthDate(int birthYear, int birthMonth, int birthDay)
{
try
{
var dateOfBirth = new DateTime(birthYear, birthMonth, birthDay);
var ageCalculator = new AgeCalculator();
if (ageCalculator.GetAge(dateOfBirth) >= AgeLimit)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
catch (ArgumentException)
{
}
return Json(false, JsonRequestBehavior.AllowGet);
}
I've been struggling some with groups in validate() but not getting anywhere.
Thanks for any kind of inputs. (I'm a front-end developer so please be more instructive if it's complicated C# code).
Kind Regards / Magnus