Solved

Need Help with Javascript to validate Month/month year for credit card

Posted on 2006-11-20
2
288 Views
Last Modified: 2012-06-27
I'm a newbie when it comes to Javascript and this what I have figured out so far. The thing I need to do is create a script that will prompt the user that they are using an credit card with an expired date, based on the current month and year. Can someone help?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<title></title>

<script type="text/javascript">
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(monthfield, yearfield){
var today=new Date()
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
var thisyear=today.getFullYear()
for (var y=0; y<20; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true)
}
</script>

<h1></h1>
<hr>
<table>
<form method="post" action="showform.php">
<fieldset id="personal">
Name:<input type="text" name="name" size="30" /> <br />

Address:<input type="text" name="address" size="30" /> <br />
Town/City:<input type="text" name="city" size="30" /> <br />
State:<select name="state" size="1">
<option>Alabama</option>
<option>Alaska</option>
<option>Alabama</option>
<option>Arizona</option>
<option>California </option>
<option>Colorado</option>
<option>Connecticut</option>
<option>Delaware</option>
<option>District of Colombia</option>
<option>Florida</option>
<option>Georgia</option>
<option>Hawaii</option>
<option>Idaho</option>
<option>Illinois</option>
<option>Indiana</option>
<option>Iowa</option>
<option>Kansas</option>
<option>Kentucky </option>
<option>Louisiana</option>
<option>Maine</option>
<option>Maryland</option>
<option>Massachusetts</option>
<option>Michigan</option>
<option>Minnesota</option>
<option>Mississippi</option>
<option>Missouri</option>
<option>Montana</option>
<option>Nebraska</option>
<option>Nevada</option>
<option>New Hampshire</option>
<option>New Jersey</option>
<option>New Mexico</option>
<option>New York</option>
<option>North Carolina</option>
<option>North Dakota</option>
<option>Ohio</option>
<option>Oklahoma</option>
<option>Oregon</option>
<option>Pennsylvania</option>
<option>Rhode Island</option>
<option>South Carolina</option>
<option>South Dakota</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Utah</option>
<option>Vermont</option>
<option>Virginia</option>
<option>Washington </option>
<option>West Virginia</option>
<option>Wisconsin</option>
<option>Wyoming</option>
</select><br />
Zip code:<input type="text" name="zip" size="5" maxlength="5" /> <br />
Telephone:<input type="text" name="Telephone" size="12" maxlength="12" /> <br />
Date of Birth:<input type="text" name "Date of Birth" size="10" maxlegth="10" /> <br />
</table>
<hr>
<table>
<p id="Credit Card type"><label>Credit Card type:</label>

<input type="radio" name="size" value="MasterCard" />MasterCard
<input type="radio" name="size" value="Visa" />Visa
<input type="radio" name="size" value="American Express" />American Express
<input type="radio" name="size" value="Discover" />Discover <br />


<center><form name=f1><table border=0><tr>
Credit Card Expiration <br />
</select>
<select id="monthdropdown">
</select>
<select id="yeardropdown">
</select>
</form>

<script type="text/javascript">
window.onload=function(){
populatedropdown("monthdropdown", "yeardropdown")
}
</script>

</table>
0
Comment
Question by:GT98
2 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17983164
Here an example:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<title></title>

<script type="text/javascript">
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(monthfield, yearfield){
  var today=new Date()
  var monthfield=document.getElementById(monthfield)
  var yearfield=document.getElementById(yearfield)
  for (var m=0; m<12; m++)
    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
  var thisyear=today.getFullYear()
  for (var y=0; y<20; y++){
    yearfield.options[y]=new Option(thisyear, thisyear)
    thisyear+=1
  }
  yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true)
}
function checkForm(theForm){
  var actDate = new Date();
  if(theForm.yeardropdown.value==actDate.getFullYear()){
    if(theForm.monthdropdown.selectedIndex<actDate.getMonth()){
      alert("Your CreditCard seams to be expired.");
      theForm.monthdropdown.focus();
      return false;
    }
  }
  return true;
}
</script>

<h1></h1>
<hr>
<table>
<form method="post" action="showform.php" onSubmit="return checkForm(this)">
<fieldset id="personal">
Name:<input type="text" name="name" size="30" /> <br />

Address:<input type="text" name="address" size="30" /> <br />
Town/City:<input type="text" name="city" size="30" /> <br />
State:<select name="state" size="1">
<option>Alabama</option>
<option>Alaska</option>
<option>Alabama</option>
<option>Arizona</option>
<option>California </option>
<option>Colorado</option>
<option>Connecticut</option>
<option>Delaware</option>
<option>District of Colombia</option>
<option>Florida</option>
<option>Georgia</option>
<option>Hawaii</option>
<option>Idaho</option>
<option>Illinois</option>
<option>Indiana</option>
<option>Iowa</option>
<option>Kansas</option>
<option>Kentucky </option>
<option>Louisiana</option>
<option>Maine</option>
<option>Maryland</option>
<option>Massachusetts</option>
<option>Michigan</option>
<option>Minnesota</option>
<option>Mississippi</option>
<option>Missouri</option>
<option>Montana</option>
<option>Nebraska</option>
<option>Nevada</option>
<option>New Hampshire</option>
<option>New Jersey</option>
<option>New Mexico</option>
<option>New York</option>
<option>North Carolina</option>
<option>North Dakota</option>
<option>Ohio</option>
<option>Oklahoma</option>
<option>Oregon</option>
<option>Pennsylvania</option>
<option>Rhode Island</option>
<option>South Carolina</option>
<option>South Dakota</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Utah</option>
<option>Vermont</option>
<option>Virginia</option>
<option>Washington </option>
<option>West Virginia</option>
<option>Wisconsin</option>
<option>Wyoming</option>
</select><br />
Zip code:<input type="text" name="zip" size="5" maxlength="5" /> <br />
Telephone:<input type="text" name="Telephone" size="12" maxlength="12" /> <br />
Date of Birth:<input type="text" name "Date of Birth" size="10" maxlegth="10" /> <br />
</table>
<hr>
<table>
<p id="Credit Card type"><label>Credit Card type:</label>

<input type="radio" name="size" value="MasterCard" />MasterCard
<input type="radio" name="size" value="Visa" />Visa
<input type="radio" name="size" value="American Express" />American Express
<input type="radio" name="size" value="Discover" />Discover <br />


<center><form name=f1><table border=0><tr>
Credit Card Expiration <br />
</select>
<select id="monthdropdown">
</select>
<select id="yeardropdown">
</select>
<input type=submit
</form>

<script type="text/javascript">
window.onload=function(){
populatedropdown("monthdropdown", "yeardropdown")
}
</script>

</table>


0
 

Author Comment

by:GT98
ID: 17983528
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now