0
Need help to validate expiray dates with javascript?
Im stuck on how to validate expiray dates that are in a select option with javascript. everytime i try it only validates the month not the year. Please help
8 Respuestas
+ 9
help us to help you by showing your code
+ 8
https://code.sololearn.com/WFE2klfCRXpf/?ref=app
something like this?
edit:
changed the approach a little bit
now, to compare dates i have extracted the text values of the year and month, create with them a new date (selectedDate) and compare the Date objects
+ 7
care to explain? ^__^
+ 7
👍
+ 4
Or at least by providing the data format to be validated ^^
0
html
Expiration Date</strong></label>
<select name="expirationmonth" id="expmonth">
<option value="1">01/ January</option>
<option value="2">02/ February</option>
<option value="3">03/ March</option>
<option value="4">04/ April</option>
<option value="5">05/ May</option>
<option value="6">06/ June</option>
<option value="7">07/ July</option>
<option value="8">08/ August</option>
<option value="9" selected>09/ September</option>
<option value="10">10/ October</option>
<option value="11">11/ November</option>
<option value="12">12/ December</option>
</select>
<select name="expirationyear" id="expyear">
<option value="1" selected>2017</option>
<option value="2">2018</option>
<option value="3">2019</option>
<option value="4">2020</option>
<option value="5">2021</option>
</select>
javascript
var date = new Date ();
var month = date.getMonth();
var year = date.getFullYear();
if(expmonth.selectedIndex<month && expyear.selectedIndex<=year) { alert("Please enter a valid expiration date"); return false;
- 1
I got it to work using those variables
<script type="text/javaScript">
function validate(orderform){
var expmonth = document.getElementById("expmonth");
var expyear = document.getElementById("expyear");
var date = new Date ();
var month = date.getMonth();
var year = date.getFullYear();
var selectedYear = expyear.options[expyear.selectedIndex].innerHTML;
var selectedMonth = expmonth.options[expmonth.selectedIndex].innerHTML.split('/')[0];
var selectedDate = new Date(selectedYear, selectedMonth, 1);
if(selectedDate < date) {
alert("Please enter a valid expiration date");
return false;
}
- 1
I used your variables plus mine and just used your example.
if(selectedDate < date) {
alert("Please enter a valid expiration date");
return false;
}
its working fine now thank you 😁