Click to See Complete Forum and Search --> : Super Simple Validation


the_groover
10-20-2003, 01:30 PM
Hi folks,

I am a newbie to JavaScirpt and I want to do some simple validation. The key is that I want to validate that a month and a year field aren't less than the current month and year. I know that this is blatently incorrect, but I wanted to get an idea from some experts that will point me in the right direction.

Thanks in advanced for your help.

-Tim

function checkccdate() {
var month = new Date.getMonth()
var year = new Date.getYear()
var yesdate = month && year

var formmonth = document.validator.cc_month.value
var formyear = document.validator.cc_year.value
var formdate = formmonth && formyear

if (yesdate < formdate){
alert ("I am sorry your Credit Card Expiration Date is invalid");
}
else (
alert("Everything looks great!...your application has been submitted")
)
}

By the way, here are the two fields from the form. (I numbered month 0 - 11 because of the getMonth thingy.

<select name="cc_month">
<option value="0" selected>Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>

<select name="cc_year">
<option value="2003" selected>2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>

Charles
10-20-2003, 02:57 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
fieldset {float:left; padding:1ex}
label {display:block; float:left; margin:0 1ex}
select {display:block}
button {clear:left}
-->
</style>

<script type="text/javascript">
<!--
function verify (form) {
if (new Date(form.year.value, form.month.value) < new Date()) {alert ('That would appear to be an invalid date.'); return false}
}
// -->
</script>

<form action="" onsubmit="return verify(this)">
<div>
<fieldset>
<legend>Date</legend>
<label>Month
<select name="month">
<option value="0" selected>Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select></label>

<label>Year
<select name="year">
<option value="2003" selected>2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select></label>
</fieldset>
<button type="submit">Submit</button>
</div>
</form>

the_groover
10-20-2003, 03:29 PM
That's the ticket.

Thanks for your help.