admin管理员组文章数量:1315307
having some trouble trying to figure this out, plete novice when it e to JavaScript. I'm trying to validate a form, more specific the expiry date of a credit card. e.g jan 2011 would be invalid but dec 2011 would be valid etc. This is what i have so far:
function ExpiryDate() {
var year
var months
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
};
my form reads:
<form id="myform" method="post" action="default.html" onsubmit="return Validate(this);">
<p>Expiration Date: Month
<select name="ExpMon" id="ExpMon" title="select a month">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">Aug</option>
<option value="9">Sept</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
Year:
<select name="ExpYear" id="ExpYear" title="select a year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select></p>
</form
I'm not to sure how to use the id's to get what I want, any help would be appreciated, thanks in advance. note:I have a function called Validate which validates a text field in my form so i could attach it to this somehow.
having some trouble trying to figure this out, plete novice when it e to JavaScript. I'm trying to validate a form, more specific the expiry date of a credit card. e.g jan 2011 would be invalid but dec 2011 would be valid etc. This is what i have so far:
function ExpiryDate() {
var year
var months
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
};
my form reads:
<form id="myform" method="post" action="default.html" onsubmit="return Validate(this);">
<p>Expiration Date: Month
<select name="ExpMon" id="ExpMon" title="select a month">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">Aug</option>
<option value="9">Sept</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
Year:
<select name="ExpYear" id="ExpYear" title="select a year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select></p>
</form
I'm not to sure how to use the id's to get what I want, any help would be appreciated, thanks in advance. note:I have a function called Validate which validates a text field in my form so i could attach it to this somehow.
Share Improve this question edited Mar 6, 2014 at 12:49 Saravanan 3731 silver badge15 bronze badges asked May 3, 2011 at 22:29 GhatziGhatzi 5972 gold badges12 silver badges24 bronze badges5 Answers
Reset to default 1var year = document.getElementById("ExpYear").value;
var month = document.getElementById("ExpMon").value;
If you prefer, you can use the names instead; you'd have to get Validate to pass the form element in as a parameter, but then you can use theForm.ExpYear.value
and theForm.ExpMonth.value
.
Note: many people dislike statements of the form if (today.getTime() > expiry.getTime()) return false; else return true;
when you can simply write return today.getTime() <= expiry.getTime();
.
Neil has answered your question about how to get the values (+1). I just wanted to add that, IMHO, it would be a better user experience not to allow your users to choose an unacceptable expiry date in the first place.
This is a little more effort but basically, by responding to the selected month you could look at the current time and then reset the allowable years based on that month. Similarly, based on the chosen year, the set of allowable months would change based on the current time.
Rather than letting the user make a mistake and then firing up a warning, just don't allow the mistake to be possible at all.
The following will take the user input, create a date object, and then pare it to today's date. One key item is that it will find the last day of the current month so that even if today is May 3 and the user select "May 2011" (which is valid for a credit card transaction), it will return "valid".
function CheckDate() {
var selectedDate = new Date (document.getElementById("ExpYear").value,document.getElementById("ExpMon").value)
var nextmonth = selectedDate.setMonth(selectedDate.getMonth() + 1);
var last_date_of_selected_date = new Date(nextmonth -1);
var today = new Date();
if (today > selectedDate) {
alert("Invalid");
}
else {
alert("Valid");
}
}
Here's a working jsFiddle to see this in action: http://jsfiddle/cPApB/3/
Try this:
var today = new Date();
var expDate = new Date($("#cart-expyear").val(),($("#cart-expmonth").val()-1)); // JS Date Month is 0-11 not 1-12 replace parameters with year and month
if(today.getTime() > expDate.getTime()) {
console.log("Your Card is expired. Please check expiry date.");
}
Use this simple validation
var expiryYear = $('#<%# DataBinder.Eval (ddlYear,"ClientID") %> option:selected').val();
var expiryMonth = $('#<%# DataBinder.Eval (ddlValidMonths,"ClientID") %> option:selected').val();
var currentUserDate = $('#<%# DataBinder.Eval (hdnFieldJsonDate,"ClientID") %>').val();
if (currentUserDate != null && expiryMonth != null && expiryYear != null) {
currentUserDate = JSON.parse(currentUserDate);
if (expiryMonth > 0 && expiryYear > 0) {
//If the Expiry Year smaller than Current Year, then validation failed
if (expiryYear < currentUserDate.Year)
{ args.IsValid = false; }
//If the Expiry Year larger than Current Year, then validation passed
else if (expiryYear > currentUserDate.Year)
{ args.IsValid = true; }
//If the Expiry Year is same as Current Year, and if Expiry Month is smaller than current month, then validation failes, else passed.
else if (expiryYear == currentUserDate.Year) {
if (expiryMonth < currentUserDate.Month)
args.IsValid = false;
else
args.IsValid = true;
}
}
else
{ args.IsValid = true; }
}
本文标签: javascriptexpiry date for month and yearStack Overflow
版权声明:本文标题:javascript - expiry date for month and year - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741962147a2407339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论