if applicant is younger than 18 in all states except alabama, display modal with validation message "Applicant must be 18 years of age to apply for credit."
if application is younger than 19 in Alabama, display modal with message: "Applicant must be 19 years of age to apply for credit."
According to your description,It seems the value of text is the form of date. So, could you please tell me which date would you like to compare with ? How 19 or 18 get ?
If you could post more details,it will help us to solve your problem quickly.
Else I suggest you to use Knockout validation. Here is the link , I hope it could help you.
According to your description, I have made a sample here. You could use substring to get the year, month, and day of the date , then it will be easy for you to compare the date .
Here is the demo , I hope it could help you
aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../Scripts/knockout-3.4.2.js"></script>
<script src="../Scripts/moment.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select data-bind="options: countries, optionsText: 'name', optionsValue: 'id', value: selectedChoice, optionsCaption: 'Choose..'"></select>
Birthday: <input type="text" data-bind="value: dateTo" placeholder="MMDDYYYY" />
<input id="Button1" type="button" value="button" data-bind="click: compare" />
</div>
<script>
var CountryModel = function(data){
var self = this;
self.id = ko.observable(data.id);
self.name = ko.observable(data.name);
};
function ViewModel() {
var self = this;
self.selectedChoice = ko.observable();
self.countries = ko.observableArray([
new CountryModel({ id: "1", name: "alabama" }),
new CountryModel({ id: "2", name: "other" })])
self.dateTo = ko.observable();
self.compare = function () {
var year = parseInt((new Date()).getFullYear()); // get the year of now
var month = parseInt((new Date()).getMonth()) + 1;// get the month of now
var day = parseInt((new Date()).getDate());// get the day of now //selectedChoice() is the id of selected option
if (this.selectedChoice() == "1") {
var b = parseInt(self.dateTo().substring(6, 10));
if (year - b < 18)// date is less
{
alert("Applicant must be 18 years of age to apply for credit")
}
if (year - b == 18)// if date is just 18 between comparing date
{
var b1 = parseInt(self.dateTo().substring(0, 2))
if (month - b1 < 0)// campare month
{
alert("Applicant must be 18 years of age to apply for credit")
}
if (month - b1 == 0)
{
var b2 = parseInt(self.dateTo().substring(3, 5))
if (day - b2 < 0) //compare day
{
alert("Applicant must be 18 years of age to apply for credit")
}
}
}
} else
{
var b = parseInt(self.dateTo().substring(6, 10));
if (year - b < 19)// date is less
{
alert("Applicant must be 19 years of age to apply for credit")
}
if (year - b == 19)// if date is just 19 between comparing date
{
var b1 = parseInt(self.dateTo().substring(0, 2))
if (month - b1 < 0)// campare month
{
alert("Applicant must be 19 years of age to apply for credit")
}
if (month - b1 == 0)//compare day
{
var b2 = parseInt(self.dateTo().substring(3, 5))
if (day - b2 < 0) // campare month
{
alert("Applicant must be 19 years of age to apply for credit")
}
}
}
}
}
}
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
</script>
</form>
</body>
</html>
None
0 Points
12 Posts
Help Knockout - date of birth validation
Feb 05, 2019 07:07 PM|srt|LINK
if applicant is younger than 18 in all states except alabama, display modal with validation message "Applicant must be 18 years of age to apply for credit."
if application is younger than 19 in Alabama, display modal with message: "Applicant must be 19 years of age to apply for credit."
self.birthDate = ko.observable(source.birthDate);
Member
370 Points
189 Posts
Re: Help Knockout - date of birth validation
Feb 06, 2019 08:09 AM|Wei Zhang|LINK
Hi srt
According to your description,It seems the value of text is the form of date. So, could you please tell me which date would you like to compare with ? How 19 or 18 get ?
If you could post more details,it will help us to solve your problem quickly.
Else I suggest you to use Knockout validation. Here is the link , I hope it could help you.
https://github.com/Knockout-Contrib/Knockout-Validation
Best Regards
Wei Zhang
None
0 Points
12 Posts
Re: Help Knockout - date of birth validation
Feb 06, 2019 01:30 PM|srt|LINK
The way they enter the DOB ....calculate from the date entered by the applicant.
Member
370 Points
189 Posts
Re: Help Knockout - date of birth validation
Feb 07, 2019 07:08 AM|Wei Zhang|LINK
Hi srt,
According to your description, I have made a sample here. You could use substring to get the year, month, and day of the date , then it will be easy for you to compare the date .
Here is the demo , I hope it could help you
aspx:
it shows as below:
Best Regards
Wei Zhang